Console c = System.console();

Is c null?

Doc:

public static Console console()

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

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

Answer from McDowell on Stack Overflow
🌐
Coderanch
coderanch.com › t › 626329 › java › readLine-method-System-Console-throwing
why readLine method of System.Console is throwing NullPointerException [Solved] (Beginning Java forum at Coderanch)
January 4, 2014 - Hello everyone, can anyone please let me know why the following code compiles fine but gives the following error at runtime. Exception in thread "main" java.lang.NullPointerException at practice.ColsoleTest.main(ColsoleTest.java:13) I will be very grateful for any help.
Top answer
1 of 5
4

From the Javadoc:

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

If there is no console associated to the JVM, the pointed line is the call of a method on a null object, hence the exception.

How do you launch your application?

Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.

If you want to read the username from the standard input, you could use this code:

try {
    System.out.print("Enter Username: ");
    InputStreamReader streamReader = new InputStreamReader(System.in);
    BufferedReader bufferedReader = new BufferedReader(streamReader);
    String username = bufferedReader.readLine();
} catch (IOException e) {
    e.printStackTrace();
}
2 of 5
3

That is because System.console()is returning null. The official documentation states (bold is mine to emphasize):

public static Console console()

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

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

You can see it here.

Find elsewhere
🌐
Coderanch
coderanch.com › t › 599308 › java › java › Buffered-reader-readLine-null-pointer
Buffered reader.readLine() null pointer exception (Beginning Java forum at Coderanch)
It seems that "reader" should not be null -or you would have gotten an exception in line 7- so it would seem that readLine returns null. Which makes sense for a file that contains only a single line - which is consumed in line 15. In other words, you're reading the file in line 15 but you're throwing away what you read. ... Thanks a lot! I hadn't realized that checking if the line was null actually consumed that line, removing that statement and just checking if the line is null using ready() did that trick!
🌐
Netjstech
netjstech.com › 2015 › 07 › how-to-read-input-from-console-keyboard-in-java.html
How to Read Input From Console in Java | Tech Tutorials
A word of caution, if running the code from eclipse, System.console() will throw null pointer exception. Please follow this discussion to know more about this exception- http://stackoverflow.com/questions/104254/java-io-console-support-in-eclipse-ide · public class ReadFromConsole { public static void main(String[] args) { //Using System.console() String username = System.console().readLine("Please enter user name : "); System.out.println("You entered : " + username); } } Output ·
🌐
Team Treehouse
teamtreehouse.com › community › consolereadline-throws-nullpointerexception-when-code-is-run-in-eclipse-ide
console.readLine throws NullPointerException when code is run in Eclipse IDE. (Example) | Treehouse Community
November 11, 2015 - I think console.readLine() returns NULL when you use it from an IDE, Eclipse in this case. Hence you get a Null Pointer Exception, when you try to do something with the result.
🌐
JetBrains
youtrack.jetbrains.com › issue › SCL-22246 › System.console-is-null
System.console is null : SCL-22246
{{ (>_<) }} This version of your browser is not supported. Try upgrading to the latest stable version. Something went seriously wrong