Kas Elvirov
Kas Elvirov

Reputation: 7980

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

Upvotes: -1

Views: 1533

Answers (2)

Bhushan Uniyal
Bhushan Uniyal

Reputation: 5703

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.

Upvotes: 3

SomeJavaGuy
SomeJavaGuy

Reputation: 7347

From the documentation:

Returns the unique Console object associated with the current Java virtual >machine, if any.

Returns: The system console, if any, otherwise null.

I guess you don´t have any Console associated with your JVM.

But you could use the System PrintStream like this:

public static void main(String[] args){
    String nbook = "Notebook";
    double price = 1.75;
    System.out.printf(Locale.ENGLISH, " 1 %10s worth %.2f euro \n", nbook, price);
}

Upvotes: 1

Related Questions