0

The below contract compiles and can be deployed. But when I try to transact using the createProject function, I obtain an error as follows:

transact to Details.createProject errored: VM error: invalid opcode.

invalid opcode

The execution might have thrown.

I am trying to create a smart contract which stores the details (name and description) of a project. My smart contract code is as follows:

pragma solidity ^0.4.17; 

contract Details {
    
    // Id number of the project 
    uint public projcount = 0;    
    
    struct Projects {
        string projectname;
        string description;
    }

    // A mapping from a student's address to projects he/she is associated with
    mapping (address => Projects[]) public Proj;
       
    // Storing the project name and description obtained from input as strings
    function createProject (string memory _projectname, string memory _description) public {
        Proj[msg.sender][projcount].projectname = _projectname;
        Proj[msg.sender][projcount].description = _description;
        projcount++;
    }
    
    // Retrieving the project details stored using student's address and project id number
    function getProject (address _addr, uint _id) public view returns (string, string) {
        return (Proj[_addr][_id].projectname, Proj[_addr][_id].description);
    } 
}

Please let me know what could be possible fix. Many thanks!

0
    // Storing the project name and description obtained from input as strings
    function createProject (string memory _projectname, string memory _description) public {
        Projects storage p;
        p.projectname = _projectname;
        p.description = _description;
        Proj[msg.sender].push(p);
        projcount++;
    }
2
  • Using the "storage" keyword gives a warning: "Uninitialized storage pointer p", but compiles. When executed, it says transaction pending and the page (remix) becomes unresponsive. I tried replacing the "storage" keyword with "memory" and it worked. But I don't know if it is correct.
    – harshav129
    Aug 23 '21 at 18:00
  • using memory instead of storage is more suitable
    – banishee
    Aug 23 '21 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.