i am working on truffle Pet-shop with react, i am using local blockchain ganache with metamask, The issue I'm encountering with my application is that when i am trying to call deleteAdopter function from UI, metamask opens and says

ALERT: Transaction Error. Exception thrown in contract code.

but if i call adopt function it executes successfully on contract and my UI changes and pet mark as adopted.

here is the code Adoption.sol

pragma solidity ^0.5.0;

contract Adoption {
    address[16] public adopters;

    // Adopting a pet
    function adopt(uint petId) public returns (uint) {
        require(petId >= 0 && petId <= 15);
        require(adopters[petId] == address(0), "already adopted pet");
        adopters[petId] = msg.sender;

        return petId;

    // Retrieving the adopters
    function getAdopters() public view returns (address[16] memory) {
        return adopters;
    //Releasing a pet 
    function deleteAdopter(uint petId) public returns(uint) {
        require(msg.sender == adopters[petId], "Only adopters can call this function");
        delete adopters[petId];
        return petId;

// Releasing a pet
//  function release(uint petId) public returns (uint) {
//    require(petId >= 0 && petId <= 15);

//    if (adopters[petId] == msg.sender) {
//      adopters[petId] = address(0);
//    }

//    return petId;
//  }

enter image description here enter image description here

1 Answer 1


Your code first declares adopters as a fixed size array of 16 addresses, but then goes on and treats it as if it was a mapping of petId to address. To use it that way, declare it as follows:

mapping (uint => address) public adopters;
  • adopters is an array of 16 addresses, it means there are 16 pets, look at the adopt function require statement; so we should not mutate this, and this is working in remix where i can call deleteAdopters and then on calling getAdopters function it returns the updated array of addresses
    – sultan
    Mar 11, 2021 at 9:19
  • Yes, instead of using an actual mapping, you are trying to map your pet IDs to the addresses by having the ID correspond to the index in the adopters array. If you really want to keep it that way, please show what is stored in adopters and the petId value, in order to find what's causing the error.
    – pwagner
    Mar 11, 2021 at 9:37
  • ok, in the adopters there are addresess of owners of pets, in the pet id the array index number is stored, so let say there are 16 pets (fixed), if someone wants to adopt a pet, the adopt function will store the address and petid will be store as the number of pet in that array, can i show you the adopterList?
    – sultan
    Mar 11, 2021 at 9:44
  • i have edited the question above, in the screenshot you can see the array of addresses
    – sultan
    Mar 11, 2021 at 9:47
  • It's still not clear what you're passing into 'adopt()' as petId. Could you disclose that too?
    – pwagner
    Mar 11, 2021 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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