Click here to Skip to main content
15,115,411 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone, Yesterday when I was reading an article on ways to read input from the user when I came across a method that said "Using Console Class."

Java
// Java program to demonstrate working of System.console()
// Note that this program does not work on IDEs as
// System.console() may require console
public class Sample {
    public static void main(String[] args)
    {
        // Using Console to input data from user
        String name = System.console().readLine();

        System.out.println("You entered string " + name);
    }
}


When I tried to run the above code on an online, interactive compiler it's giving the error: Exception in thread "main" java.lang.NullPointerException at Sample.main(Sample.java:8)

Why is this happening?

I also tried to create an object of Console class and then call the .readLine() function but it gave me the same error.

What I have tried:

I just run the program in online compiler and getting some error
Posted
Updated 4 days ago
v2
Comments
Richard Deeming 4 days ago
   
I have removed the link to the "interview questions site" online compiler, since it made your question look like spam. A simple Google search will find many online Java compilers, if anyone needs one.
Richard MacCutchan 4 days ago
   
I have tested this locally and it works. The issue is most likely to do with using the online web service.

1 solution

Maybe you should use System.in, since the console does not seem to be available.
There is a clear example of basic I/O just below compiler's window.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900