javanullpointerexceptionjava.lang.class

java.lang.NullPointerException - System.console()


I can't solve the problem two hours. Please help me.

My code:

   package work.anart;
     import java.io.Console;
     public class Office {
         public static void main(String[] args) {
             Console cons = System.console();
             cons.printf("\n");
                 String nbook = "Notebook";
                 double price = 1.75;
             cons.printf(" 1 %10s worth %.2f euro \n", nbook, price);
             cons.printf(" 1 %10s worth %.2f euro \n", nbook, price);
         }
     }

Run:

Exception in thread "main" java.lang.NullPointerException
    at work.anart.Office.main(Office.java:8)
Java Result: 1

Solution

  • Your program is working well for me without any errors.I think you are using version less then java 1.7,please use 1.7 or 1.8 because java.io.Console was introduced in java 1.7,I feel you should upgrade and check a simple code on your console/terminal.