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?

andandandand
  • 21,946
  • 60
  • 170
  • 271

7 Answers7

17
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
  • 107,573
  • 31
  • 204
  • 267
  • Oh...how can I get a console object from Netbeans? – andandandand Dec 16 '09 at 22:41
  • 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 Dec 16 '09 at 22:43
4

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

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
  • 1
    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 Dec 16 '09 at 23:03
4

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
  • 1,448
  • 1
  • 17
  • 19
4

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();
Joe Cheng
  • 8,804
  • 3
  • 26
  • 25
  • 1
    This is an alternate, not an answer. People try to use Console for a reason, and it is for readPassword – Kris Swat Mar 18 '20 at 11:42
2

Is c null somehow?

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

EMP
  • 59,148
  • 53
  • 164
  • 220
2

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

Cameron
  • 1,868
  • 3
  • 21
  • 38
1

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 Maman
  • 30,277
  • 10
  • 88
  • 118