andandandand
andandandand

Reputation: 22270

NullPointerException in Console's readLine()

This:

Console c = System.console();
        String readline;
        String u = c.readLine("%s", "args");

Throws a NullPointerException. Yet the signature of the method is:

 public String readLine(String fmt, Object... args)

Why's this exception being thrown?

Upvotes: 6

Views: 24291

Answers (7)

Joe Cheng
Joe Cheng

Reputation: 9524

Because System.console() is null in the IDE you are using. Try java.util.Scanner instead:

import java.util.Scanner;
Scanner s = new Scanner(System.in);
String u = s.nextLine();

Upvotes: 4

greatghoul
greatghoul

Reputation: 1538

via: http://www.codeguru.com/forum/showthread.php?t=487190 for detail

Before using a method it always pays to read the API documents on what the method does. For example docs for the console() method say:

Quote:

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 this virtual machine has a console then it is represented by a unique instance of this class which can be obtained by invoking the System.console() method. If no console device is available then an invocation of that method will return null.of that method will return null.

If you try invoking the program from the command line using the java command then it will have a console and the method should not return null.of that method will return null.

Alternatively, using the Scanner class will work inside of your IDE:

Scanner sc = new Scanner(System.in);

Upvotes: 4

Cameron
Cameron

Reputation: 1918

System.console() returned null, it is the only line in that code snippet that could have possibly thrown a null pointer exception.

Upvotes: 2

Itay Maman
Itay Maman

Reputation: 30723

There's something strange in the code snippet. You declare a variable called "readline" but you don't initialize it and don't use it.

Is it possible that in the program you somehow use this variable w/o initializing it? (a long shot, I know)

Upvotes: 1

EMP
EMP

Reputation: 61971

Is c null somehow?

By the way, your readLine statement is equivalent to c.readLine("args") - is that what you intend?

Upvotes: 2

Carl Manaster
Carl Manaster

Reputation: 40336

NullPointerException is a RuntimeException, which means it doesn't have to be declared in the method signature.

Upvotes: 4

McDowell
McDowell

Reputation: 108879

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.

Upvotes: 17

Related Questions