-3

my code was:

package javaapplication1;


import java.io.Console;
public class JavaApplication1 {


    public static void main(String[] args) {

        Console console =System.console();
        // TODO code application logic here
        String ageInString ;
        ageInString= console.readLine("how old are you?  ");
        int age = Integer.parseInt(ageInString);
        if(age<18)
        {console.printf("not valid");
        System.exit(0);
        }

    }

}

and the error is:

Exception in thread "main" java.lang.NullPointerException
    at javaapplication1.JavaApplication1.main(JavaApplication1.java:20)
Java Result: 1
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60

1 Answers1

1

That means that you are referencing an object that doesn't exist. In this case it is most likely to be ageInString or age. Try adding checkpoints while debugging, it is a good way to debug. Add something like assert ageInString != null after ageInString = console.readLine("how old are you?"); and same for age before comparing to 18.

Zoe
  • 27,060
  • 21
  • 118
  • 148
John
  • 21
  • 3