tags:

views:

499

answers:

6

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?

+9  A: 
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.

McDowell
Isn't that a factory method?
omgzor
Oh...how can I get a console object from Netbeans?
omgzor
I haven't tried using this call in Netbeans, but you can use remote debugging in Eclipse to get round this: http://stackoverflow.com/questions/104254/java-io-console-support-in-eclipse-ide/105403#105403 I'm sure it is possible to do something similar in Netbeans.
McDowell
+4  A: 

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

Carl Manaster
It is an answer to the question of why the NPE is thrown, given that the signature doesn't specify any exceptions. As I read the question, that appeared to be the questioner's main point of confusion.
Carl Manaster
+2  A: 

Is c null somehow?

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

Evgeny
A: 

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)

Itay
this is a textbook sample.
omgzor
+1  A: 

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

Cameron
A: 

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);

GreatGhoul