我是初学者。只是想知道为什么这段代码在 Windows 命令提示符下工作得很好,但我得到一个:
Exception in thread "main" java.lang.NullPointerException
at Test1.main(Test1.java:13)
eclipse 错误。这在我身上发生过很多次,它真的阻止了我使用 eclipse。
代码如下:
导入java.io.Console;
public class Test1 {
public static void main(String[] args) {
Console myConsole = System.console();
for (int a = 0; a < 10; a++){
int a2 = a * a;
myConsole.printf("\n%d squared is: %d.",a,a2); //Problem with this line
}
System.exit(0);
}
}
最佳答案
System.console()
状态的 javadoc
Returns The system console, if any, otherwise null.
Eclipse 不得关联系统控制台。
改用 System.in
,可能使用 java.util.Scanner
作为输入。和 System.out
用于输出。
关于java - Eclipse 中的 NullPointerException 但不在命令提示符中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19452132/