1

I have a menu system that will print various information about an ArrayList. In my third case I have to call a method that prints the size of the arraylist. The method that I am trying to call is part of the same class. These are the error messages that I am receiving. How would I resolve these?

error:

non-static method printrecordnumber(ArrayList<vehicle>) cannot be referenced from a static context
   case 3:  System.out.println("Number of records: " + printrecordnumber(vehiclearray));

error:

'void' type not allowed here
   case 3:  System.out.println("Number of records: " + printrecordnumber(vehiclearray));

Here is the code I have:

//Menu System
do{
    System.out.println("Enter number of option or stop to stop the program\n1. Print all data\n2. Print all data (Sorted)\n3. Print number of records\n4. Print Bicycles and Trucks from the sorted data\n5. Print vehicles from area code 987");

    try{
        String option = input.next();

        if((option.equals("stop"))) {
            System.exit(0);
        }

        int optionint = Integer.parseInt(option);

        switch (optionint){
            case 1: 
                break;
            case 2: 
                break;
            case 3:  
                System.out.println("Number of records: " + printrecordnumber(vehiclearray)); 
                break;
            case 4: 
                break;
            case 5: 
                break;
       }
    }
    catch (NumberFormatException e){}
}while(true);

//Main Method Ending Bracket
}


public void printrecordnumber(ArrayList<vehicle> vehiclearray){
   System.out.println(vehiclearray.size());
}
Matthew S.
  • 711
  • 1
  • 5
  • 22
Fluxxxy
  • 347
  • 1
  • 3
  • 13
  • Do you know the difference between static and non-static methods? – Kakalokia Oct 19 '13 at 18:05
  • First error: You are calling a non-static method from a static block. You need to understand difference between static and non static. – meghamind Oct 19 '13 at 18:06
  • Second error: When a method returns void there is nothing to print, printrecordnumber already prints, you should just call that directly. – meghamind Oct 19 '13 at 18:08
  • I have a vague idea of the difference. My professor mentioned it in passing. – Fluxxxy Oct 19 '13 at 18:09
  • A static method will exist as long as your class exists. This means it relates to your class, not to an object of your class. A normal method, such as printrecordnumber can only be called on an object of the class it is defined in. The way I'd solve the first problem is by creating an object of the class where printrecordnumber is defined, and calling the method on that object. – Kakalokia Oct 19 '13 at 18:12

8 Answers8

1

In the first error, printrecordnumber is a void method. Therefore, you need to create an instance of the class and use it to access the method e.g. x.printrecordnumber. As for the second exception, you need to make printrecordnumber a return method to be able to use it in case 3

oluwaremieo
  • 41
  • 1
  • 7
  • +1 The only answer which mentions creating an instance to call the method rather than making it static. – Kakalokia Oct 19 '13 at 18:16
  • This one does, and was written before all these quick short answers stormed in: http://stackoverflow.com/questions/19469250/passing-an-arraylist-to-method-to-print-size/19469388#19469388 @AliAlamiri – Matthew S. Oct 19 '13 at 18:19
  • @AliAlamiri I disagree. If all `printrecordnumber` does is to return the size of the List, why not make it a static. It saves creating a new Object, just for the purpose of getting the size of the list, i.e, unless, OP has this method in some Object which can be extended and `printrecordnumber` can be overridden. – Srikanth Reddy Lingala Oct 19 '13 at 18:24
1

error: non-static method printrecordnumber(ArrayList) cannot be referenced from a static context case 3: System.out.println("Number of records: " + printrecordnumber(vehiclearray));

"A static method belongs to the class itself and a non-static (or instance) method belongs to each object that is generated from that class. If your method does something that doesn't depend on the individual characteristics of its class, make it static (it will make the program's footprint smaller). Otherwise, it should be non-static." - Static method vs. Non static methods

To fix this error just make the method static:

public static void printrecordnumber(ArrayList<vehicle> vehiclearray){
    System.out.println(vehiclearray.size());
}

Now on to the second error:

error: 'void' type not allowed here case 3: System.out.println("Number of records: " + printrecordnumber(vehiclearray));

Void means that the method does not return anything, but in System.out.println it requires a String. You cannot simply put a System.out.println in a System.out.println. To fix this just make your method return a int (ints have an automatic toString() method built in) instead of printing to the terminal:

public static int printrecordnumber(ArrayList<vehicle> vehiclearray){
   return vehiclearray.size();
}

Hope this helps!

Matthew S.
  • 711
  • 1
  • 5
  • 22
1

This error is due to follows reasons:

  1. Return type of your printrecordnumber() is void
  2. you are callin a non static method from a static method (main).

Change your method as follows and it will work

public static int printrecordnumber(ArrayList<vehicle> vehiclearray){
    return vehiclearray.size();
}
Shamse Alam
  • 1,285
  • 10
  • 21
0

error: non-static method printrecordnumber(ArrayList) cannot be referenced from a static context case 3: System.out.println("Number of records: " + printrecordnumber(vehiclearray));

You cannot call a non-static method from a static method. Here's more on why you can't do that. printrecordnumber() should be a static method. Like this:

public static void printrecordnumber(ArrayList<vehicle> vehiclearray){
   System.out.println(vehiclearray.size());
}

error: 'void' type not allowed here case 3: System.out.println("Number of records: " + printrecordnumber(vehiclearray));

you cannot append a void to a String. In other words, printrecordnumber() should return an Object. You can return the size of List from printrecordnumber

public static int printrecordnumber(ArrayList<vehicle> vehiclearray){
   return vehiclearray.size();
}
Community
  • 1
  • 1
  • This fixed my problem. Would I continue to declare it this way in another method if I wanted to return the toString() methods for the objects in my ArrayList? – Fluxxxy Oct 19 '13 at 18:11
  • You would replace `int` with `string` @user2897124 – Matthew S. Oct 19 '13 at 18:34
  • @Matthew Why would that be necessary? – Srikanth Reddy Lingala Oct 19 '13 at 18:37
  • @SrikanthLingala As I said in my answer below, the place where the question has `void` or where this answer has `int` (This answer is right for the original question), is the place where you describe the object type that will be returned when you call a method. @user2897124 in his comment was asking about how to set it up to return other objects types. And the answer to that question is you can set the method return any objects type you want, including strings. – Matthew S. Oct 19 '13 at 18:51
  • @Matthew Sorry, I thought your comment was directed to my answer. But to be frank, I did not quite understand the question of OP in the first comment to this answer. – Srikanth Reddy Lingala Oct 19 '13 at 19:00
0

Change return type of printrecordnumber to int. And make it static to refer from other static method. use

public static int printrecordnumber(ArrayList<vehicle> vehiclearray)
{
    return vehiclearray.size();
}
codingenious
  • 8,385
  • 12
  • 60
  • 90
0

Your function printrecordnumber must return an integer.

Else you cannot pass it within the print function, since it doesn't return any value.

Second, printrecordnumber should be

public static int
Se Won Jang
  • 773
  • 1
  • 5
  • 12
0

you'r printing void, return a String (or int, or long) in printrecordnumber instead of printing it to the console.

furthermore, you'r probably calling the method out of a static method, either create an object, or make the method printrecordnumber static as well

divadpoc
  • 903
  • 10
  • 31
0

Try this insted:

    case 3:  
        System.out.print("Number of records: ");
        printrecordnumber(vehiclearray);
        break;

and

public static void printrecordnumber(ArrayList<vehicle> vehiclearray){
   System.out.println(vehiclearray.size());
}

In the first case, you can't concatenate a void with a string and in the second case, if you don't have an object your method acts on (ex John.getAge()) the method has to be static.

You could also return an int in your method and then you could concatenat it with the string like this:

    case 3:  
        System.out.print("Number of records: " + printrecordnumber(vehiclearray));
        break;

and

public static int printrecordnumber(ArrayList<vehicle> vehiclearray){
   return vehiclearray.size();
}
Dom
  • 1,687
  • 6
  • 27
  • 37