-1

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
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Kas Elvirov
  • 7,394
  • 4
  • 40
  • 62
  • If you've spent 2 hours on it you can surely tell us more about it than pasting the code. – runDOSrun Jul 16 '15 at 14:47
  • possible duplicate of [Java: How to get input from System.console()](http://stackoverflow.com/questions/4644415/java-how-to-get-input-from-system-console) – Dan Getz Jul 16 '15 at 14:49

2 Answers2

3

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.

Bhushan Uniyal
  • 5,575
  • 2
  • 22
  • 45
  • I wrote this code. `import java.io.Console; public class Test { public static void main(String[] args) throws Exception { Console console = System.console(); if (console == null) { System.out.println("Unable to fetch console"); return; } String line = console.readLine(); console.printf("I saw this line: %s", line); } }` He said me : `Unable to fetch console` – Kas Elvirov Jul 17 '15 at 12:47
  • I have Java(TM) SE Runtime Environment (build 1.8.0_45-b14) – Kas Elvirov Jul 17 '15 at 12:48
  • Artem i think you are using eclipse tool or may be another tool to run this program , because eclipse runs your program as a background process and not as a top-level process with System console or depend on diffrent diffrent IDE handling of console io. You can use Scanner class like import java.util.Scanner; String data; Scanner scanInput = new Scanner(System.in); data= scanInput.nextLine(); scanInput.close(); System.out.println(data); – Bhushan Uniyal Jul 20 '15 at 09:25
1

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);
}
Community
  • 1
  • 1
SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
  • This code is working but Me need use System.console(). In the book, the task on this topic – Kas Elvirov Jul 16 '15 at 15:01
  • I wrote this code. `import java.io.Console; public class Test { public static void main(String[] args) throws Exception { Console console = System.console(); if (console == null) { System.out.println("Unable to fetch console"); return; } String line = console.readLine(); console.printf("I saw this line: %s", line); } }` He said me : `Unable to fetch console ` – Kas Elvirov Jul 17 '15 at 13:38
  • I have Java(TM) SE Runtime Environment (build 1.8.0_45-b14) – Kas Elvirov Jul 17 '15 at 13:39