Java 控制台的 readLine() 中的 NullPointerException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1918297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:25:01  来源:igfitidea点击:

NullPointerException in Console's readLine()

javaconsole

提问by andandandand

This:

这个:

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

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

抛出一个NullPointerException.Yet 方法的签名是:

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

Why's this exception being thrown?

为什么会抛出这个异常?

采纳答案by McDowell

Console c = System.console();

Is cnull?

是否为c空?

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.

公共静态控制台控制台()

返回与当前 Java 虚拟机关联的唯一控制台对象(如果有)。

返回: 系统控制台(如果有), 否则为 null

回答by Carl Manaster

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

NullPointerException 是一个 RuntimeException,这意味着它不必在方法签名中声明。

回答by EMP

Is cnull somehow?

c不知何故为空?

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

顺便说一句,您的 readLine 语句相当于c.readLine("args")- 这是您的意图吗?

回答by Itay Maman

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.

代码片段中有一些奇怪的东西。您声明了一个名为“readline”的变量,但您没有初始化它,也没有使用它。

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

是否有可能在程序中你以某种方式使用这个变量而不初始化它?(远射,我知道)

回答by Cameron

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

System.console() 返回 null,它是该代码片段中唯一可能引发空指针异常的行。

回答by greatghoul

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

通过:http: //www.codeguru.com/forum/showthread.php?t=487190了解详情

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:

在使用方法之前,阅读有关该方法功能的 API 文档总是值得的。例如, console() 方法的文档说:

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.

虚拟机是否有控制台取决于底层平台以及调用虚拟机的方式。如果虚拟机从交互式命令行启动而不重定向标准输入和输出流,那么它的控制台将存在并且通常将连接到启动虚拟机的键盘和显示器。如果虚拟机是自动启动的,例如由后台作业调度程序启动,那么它通常没有控制台。

如果此虚拟机具有控制台,则它由此类的唯一实例表示,该实例可通过调用 System.console() 方法获得。如果没有可用的控制台设备,则调用该方法将返回 null。该方法将返回 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.

如果您尝试使用 java 命令从命令行调用程序,那么它将有一个控制台并且该方法不应返回 null。该方法将返回 null。

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

或者,使用 Scanner 类将在您的 IDE 中工作:

Scanner sc = new Scanner(System.in);

Scanner sc = new Scanner(System.in);

回答by Joe Cheng

Because System.console()is nullin the IDE you are using. Try java.util.Scannerinstead:

因为System.console()null在你正在使用的IDE。试试吧java.util.Scanner

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