$0.09

invalid address (arg="", coderType="address", value=null)

Ask a question+
0

I am trying to run my dapp on browser but the error comes up every time. I have metamask connected with Ganache and I am able to transfer ether from one account to another account using metamask.

The Election sol file:


pragma solidity 0.5.16;  

contract Election {
    string public candidate;

   event votedEvent(uint indexed _candidateId);

  struct Candidate{
     uint id;
     string name;
     uint voteCount;
     }

   mapping(uint=> Candidate) public candidates;
   mapping(address=> bool) public voters;

   uint public candidatesCount;

    constructor () public {
        addCandidate("Candidate 1");
        addCandidate("Candidate 2");
    }


    function addCandidate (string memory _name) private{   
       candidatesCount++;
       candidates[candidatesCount] = Candidate(candidatesCount,_name,0);
}

    function vote (uint _candidateId) public{
       require(!voters[msg.sender]);
       require(_candidateId> 0 && _candidateId <= candidatesCount);
       voters[msg.sender]=true;
       candidates[_candidateId].voteCount++;
       emit votedEvent(_candidateId);
}

}

The app.js file :



App = {
  web3Provider: null,
  contracts: {},
  account: '0x0',

  init: function() {
    return App.initWeb3();
  },


  initWeb3: function() {
 if (typeof web3 !== 'undefined') {

      App.web3Provider = web3.currentProvider;
      web3 = new Web3(web3.currentProvider);
    } else {

      App.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');
      web3 = new Web3(App.web3Provider);
    } 
   return App.initContract();
  },

 initContract: function() {


    $.getJSON("Election.json", function(election) {

      App.contracts.Election = TruffleContract(election);

      App.contracts.Election.setProvider(App.web3Provider);

      App.listenForEvents();

      return App.render();
    });
  },


  render: function() {
    var electionInstance;
    var loader = $("#loader");
    var content = $("#content");

    loader.hide();
    content.hide();

    web3.eth.defaultAccount = this.account;
    web3.eth.getCoinbase(function(err, account) {
      if (err === null) {
        App.account = account;
        $("#accountAddress").html("Your Account: " + account);
      }

    });

    App.contracts.Election.deployed().then(function(instance) {
      electionInstance = instance;
      return electionInstance.candidatesCount();
    }).then(function(candidatesCount) {
      var candidatesResults = $("#candidatesResults");
      $('#candidatesResults').empty();

      var candidatesSelect = $('#candidatesSelect');
      $('#candidatesSelect').empty(); 

     for (var i = 1; i <= candidatesCount; i++) {  
        electionInstance.candidates(i).then(function(candidate) {
          var res;
          var id = candidate[0];
          var name = candidate[1];
          var voteCount = candidate[2]; 

          candidatesResults.empty();
          var candidateTemplate = "<tr><th>" + id + "</th><td>" + name + "</td><td>" + voteCount + "</td></tr>"
          candidatesResults.append(candidateTemplate);     

          ca1=voteCount;
          na1=name;


          var candidateOption = "<option value='" + id + "' >" + name + "</ option>"
            candidatesSelect.append(candidateOption);  

        });
       }
             return electionInstance.voters(App.account);
    }).then(function(hasVoted) {
    if(hasVoted) {
      $('form').hide();
    }     
      loader.hide();
      content.show();
    }).catch(function(error) {
      console.warn(error);
    });  
  },


 castVote: function() {
    var candidateId = $('#candidatesSelect').val();
    App.contracts.Election.deployed().then(function(instance) {
      return instance.vote(candidateId, { from: App.account });
    }).then(function(result) {
      // Wait for votes to update
      $("#content").hide();
      $("#loader").show();
    }).catch(function(err) {
      console.error(err);
    });
   },

 listenForEvents: function() {
  App.contracts.Election.deployed().then(function(instance) {

 instance.events.votedEvent({
    fromBlock: 0,
    toBlock: 'latest'
 }, function(error, event){ console.log(event); })
 .on('data', function(event){
    App.render();
 })
 .on('changed', function(event){
    // remove event from local database
 })
 .on('error', console.error);
    });
 }
  };

$(function() {
  $(window).on('load', function () {
    App.init();
  });
});
add comment

0 Answer

Your Answer