Please explain how to reference a method (getAverage) from static context

#1 error: non-static method getAverage(ArrayList) cannot be referenced from a static context
1 error

public int getAverage (ArrayList<Integer> grades) {
  if (grades.size() < 1) {
      System.out.println("Grade list is empty.");
      return 0;
  	} else {
      	int sum = 0;  
    for (Integer grade : grades) {
      sum += grades.get(grade);
      int average = sum/grades.size();	
      return average;
public static void main(String [] args) {
  ArrayList<Integer> myClassroom = new ArrayList <Integer>();
  GradeAnalyzer myAnalyzer = new GradeAnalyzer();


You'd need to make the method static or obtain an object of that class


Thanks for your help. I made the getAverage method static. Now I am now getting IndexOutOfBoundsException.
Any suggestions? I will look through a JAVA book that I own and I may go back through the Array and HashMap lesson.

public static int getAverage (ArrayList<Integer> grades) {

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 98, Size: 7
at java.util.ArrayList.rangeCheck(
at java.util.ArrayList.get(
at GradeAnalyzer.getAverage(
at GradeAnalyzer.main(


For that you'd find that line in the code and print out the index you're using and the length of the whatever-it-is to confirm that it's outside and then reconsider how you're producing the indexes or if perhaps it's not the right iterable value

If there's something specific you want to know about what's going on there then googling is probably both faster and more reliable than trying to find it in a book. It's also more of a look-at-what's-going-on thing


Thanks for your response. I will look into passing arrays to methods and printing arrays.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.