-3

The method calls at the end of the main method are giving me an error saying "non-static method cannot be referenced from a static context" I'm not sure what I'm doing wrong in the method call.

    public static void main(String[] args) 
 { 
   ArrayList<Candidate> voteCount = new ArrayList<Candidate>();
   //add objects to voteCount

   printListResults(voteCount);
   totalListVotes(voteCount);
   printListTable(voteCount);
}
 public void printListResults(ArrayList<Candidate> election) 
{ 
    //some code
}
public int totalListVotes(ArrayList<Candidate> election) 
{ 
    //some code
}
public void printListTable(ArrayList<Candidate> election) 
{ 

        //some code
    }
Andrew McAvoy
  • 21
  • 1
  • 6

2 Answers2

1

You simply need to declare these methods as static

public static void printListResults(ArrayList<Candidate> election) { 
    //some code
}
public static int totalListVotes(ArrayList<Candidate> election) { 
    //some code
}
public static void printListTable(ArrayList<Candidate> election) { 
    //some code
}

An alternative approach would be to instantiate an object of your class, as pointed out in the answer from JoschJava. Either way will work. Which approach you choose is partly a matter of taste and partly depends upon the needs of your application (which is beyond the scope of this question).

EJK
  • 12,332
  • 3
  • 38
  • 55
0

The problem is that you're trying to call a class method from a static method. You need to instantiate your class:

YourClass classObj = new YourClass();
classObj.printListResults(voteCount);
JoschJava
  • 1,152
  • 12
  • 20