1

i start reading "java core" book and when i try to compile and run this code

import java.io.Console;
public class Test {

public static void main(String[] args) {

    Console cons = System.console();
    String name = cons.readLine("enter user name: ");
    System.out.println(name); } }

first with netbeans 9.0 i get this error

Exception in thread "main" java.lang.NullPointerException
at Test.main(Test.java:8)
/home/ahmed/NetBeansProjects/Test/nbproject/build-impl.xml:1328: 
The following error occurred while executing this line:
/home/ahmed/NetBeansProjects/Test/nbproject/build-impl.xml:948: Java returned: 1
BUILD FAILED (total time: 0 seconds)

but when i try to compile and run the same code with javac Test.java then java Test no errors are generated and the code runs perfectly what does this means and how to fix it ?

azro
  • 53,056
  • 7
  • 34
  • 70
ahmedES
  • 49
  • 6
  • try eclipse ide... maybe ur in a wrong src folder ? – tgkprog Dec 09 '18 at 21:03
  • You need to seriously read the [`Console` JavaDocs](https://docs.oracle.com/javase/10/docs/api/java/lang/System.html#console()) which states *"The system console, if any, otherwise null."*. When you've read that, you need to go and read [Java Console](https://docs.oracle.com/javase/7/docs/technotes/guides/jweb/jcp/console.html) to understand exactly what the Console is – MadProgrammer Dec 09 '18 at 21:03

1 Answers1

1

from the javadoc of System.console()

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

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

The console/terminal of Netbeans doesn't support it, so System.console() returns null.

k5_
  • 5,450
  • 2
  • 19
  • 27
  • aha i see it now, i think the writer should mention this since he had suggested netbeans at the first chapter and did a small tutorial in how to use it to – ahmedES Dec 09 '18 at 21:11