-2

Hello All I'm writing some code and keep getting a Null Pointer Exception and for some reason I'm just hitting a mental wall.

Now I do know that null pointer is when the value its trying to use is null, but I can't seem to figure out how to fix the problem. (Errored Line is *'d out.)

   public static void getMenuChoice(Dog s) {
      int LCV = 0;
     Scanner in = null; // create a scanner object
  do {
     System.out.println("Welcome to the Input Menu");
     System.out.println("1: Assign the name");
     System.out.println("2: Assign the owner");
     System.out.println("3: Assign the age");
     System.out.println("4: Assign the weight");
     System.out.println("5: Assign the breed");
     System.out.println("6: Assign the time");
     System.out.println("7: Display Dog Information");
     System.out.println("8: Quit");
     System.out.print("Choose a Menu Option => ");


     ****************int input = in.nextInt();*********************

     switch (input) {
        case 1:
           System.out.println("Please Enter the Dogs name");
           s.setDogName(in.next());
           break;
        case 2:
           System.out.println("Please Enter the Owners name");
           s.setOwnerName(in.next());
           break;
        case 3:
           System.out.println("Please Enter the Dogs age");
           s.setDogAge(in.nextDouble());
           break;
        case 4:
           System.out.println("Please Enter the Dogs weight");
           s.setWeight(in.nextDouble());
           break;
        case 5:
           System.out.println("Please Enter the Dogs breed");
           s.setDogName(in.next());
           break;
        case 6:
           System.out.println("Please Enter the Time");
           s.setTime(in.nextDouble());
           break;
        case 7:
           System.out.println(s);
           break;
        case 8:
           LCV = 8;
           break;
     }
  } while (LCV != 8);

}

tmp
  • 1,079
  • 9
  • 16
SynGaren NA
  • 21
  • 1
  • 4

2 Answers2

2

Your Scanner not initialized ,you shoud do

Scanner in = new Scanner(System.in);  // instead of  Scanner in = null;

So when you are doing

in.nextInt();   // null.nextInt()

you are getting NullPointerException

public class NullPointerException extends RuntimeException

Thrown when an application attempts to use null in a case where an object is required. These include:

  • Calling the instance method of a null object.
  • Accessing or modifying the field of a null object.
  • Taking the length of null as if it were an array.
  • Accessing or modifying the slots of null as if it were an array.
  • Throwing null as if it were a Throwable value.

Docs

Community
  • 1
  • 1
singhakash
  • 7,891
  • 6
  • 31
  • 65
1

A NullPointerException means you are dereferencing a variable which has not been instantiated (a variable which points to null). Here's your declaration of in:

Scanner in = null; 

You have a null variable here, which you then dereference:

int input = in.nextInt();

This causes your NullPointerException. You need to instantiate in somehow like

Scanner in = new Scanner(System.in);
Kon
  • 10,702
  • 6
  • 41
  • 58