8

I have tried code:

import java.io.Console;
public class Default
{
    public static void main(String args[]) throws IOException
    {
        Console console = System.console();
        String testing = console.readLine("Enter Name: ");
        System.out.println("Entered Name: "+ testing);
    }
}

goes to exception with following error:
Source not found. NullPointerException

I am using Eclipse Juno EE for debugging .. !

And the reference link for above written code is here

5

5 Answers 5

6

Are you running your program from an ide as console.readLine returns null when used from an IDE.

For more details refer to this

If you run it from command line you will not get this error.

Sign up to request clarification or add additional context in comments.

1 Comment

In my case in Linux console when i run the script from run.sh i got NullPointerException after removing "&" from end of my script and remove nohup from the beginning the error gone away. good-luck
3

System.console() returns null if there is no console.

You can work round this either by adding a layer of indirection to your code or by running the code in an external console and attaching a remote debugger.

2 Comments

Exactly - and, this is true when running from within Eclipse: bugs.eclipse.org/bugs/show_bug.cgi?id=122429
I tried the same in netbeans, System.console() returns null even here. Why is it so?
3

That is because, IDE is not using console !

Go to cmd.exe

type cd <bin path> hit enter..

now type java <classname> hit enter

It works!

2 Comments

Well, you're right. It works from windows console. But the problem here is, OP is using Eclipse IDE. And Eclipse IDE does not have a System.console(). It's a known bug: - bugs.eclipse.org/bugs/show_bug.cgi?id=122429
@RohitJain, I said the same thing his IDE doesn't use console. But you can run any java compiled code from command prompt. That's the beauty of Java!
0
import java.io.*;

public class ConsoleExTest {

    public static void main(String[] args) throws Exception {
        Console c = System.console();
        String uname = c.readLine("User Name:");
        char[] pwd = c.readPassword("Password:");
        String upwd = new String(pwd);
        if (uname.equals("chenna") && upwd.equals("chenna")) {
            System.out.println("User is valid");
        } else {
            System.out.println("User is not valid");
        }
    }

}

Note:

System.console(); return null so we will get

Comments

0

The cause of this problem has already been mentioned in other answers, but I would like to add one possible solution.

There is a library that can be used as a replacement for java.io.Console that mitigates the problem: https://codeberg.org/marc.nause/console-with-fallback

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.