NullPointerException in readLine () console

It:

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

Returns a NullPointerException.However, the method signature:

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

Why is this exception selected?

+5
source share
7 answers
Console c = System.console();

Is cnull?

Doc :

public static console console ()

Returns a unique console object associated with the current Java virtual machine, if any.

Returns: System console, if any, otherwise null .

+17
source

NullPointerException is a RuntimeException, which means that it should not be declared in the method signature.

+4
source

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

, , API , . , docs console() :

:

, . , , . , , .

, System.console(). null. null.

java, , null.of null.

, Scanner IDE:

Scanner sc = new Scanner(System.in);

+4

c null -?

, readLine c.readLine("args") - , ?

+2

System.console() null, , , , .

+2

- . , "readline", .

Is it possible that in a program you somehow use this variable without initialization? (long shot, I know)

+1
source

Because System.console()- nullin your IDE. Try instead java.util.Scanner:

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

All Articles