1

When I try to pass address as parameter to this addEmployee function it gives me this error:

invalid opcode
    The execution might have thrown.
    Debug to get more information.
modifier onlyOwner()
{
    require(msg.sender==owner,"msg.sender is not the Owner");
    _;
}

function addEmployee(address payable employeeWallet) onlyOwner external
{
    emp[emp.length-1] = employeeWallet;
}
  • An Invalid Opcode exception is the result of either one of two things: 1. An assert(cond) statement, where cond evaluates to false. 2. An array[n] statement, where n exceeds the boundaries of the array (i.e., n < 0 || n >= array.length). So when you get this type of exception, always look for either one of those two "things" in your code. – goodvibration Sep 30 '19 at 8:56
0

(This is just an educated guess, to be 100% certain I'd need to see your entire contract and the series of calls leading up to the error)

An INVALID opcode is raised when you attempt to access a storage location that hasn't been intialized. In this case, you are attempting to store data in a dynamic array emp at location emp.length-1. Because the length of emp is zero, the calculation emp.length-1 underflows and the EVM reverts when it cannot access emp[2^256-1].

As an alternative, I suggest using emp.push(employeeWallet) for appending the new value to the array.

Useful reading:

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.