【发布时间】:2014-07-25 09:22:59
【问题描述】:
为什么我在运行此代码时收到 NullPointerException,我查了它是什么但仍然不明白如何修复它;请帮忙?
import java.util.Scanner;
public class Test{
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
System.out.println();
String User = sc.nextLine();
char [] pass = System.console().readPassword();
System.out.println(pass);
char c = sc.next().charAt(0);
System.out.println(c);
char d = sc.findInLine("a").charAt(0);
System.out.println(d);
char b = sc.next().charAt(0);
System.out.println(b);
System.out.println(User);
}
}
运行代码时一切正常,直到最后两行使用变量char b 输出的错误如下:
Exception in thread "main" java.lang.NullPointerException
at Test.main(Test.java:11)
【问题讨论】:
-
什么是“sc”,你如何确定它有两个“.”?
-
因为
sc.next(".")返回null。 -
第 33 行是
char d = sc.findInLine(".").charAt(0); -
看起来
sc.findInLine(".")返回null。 -
@Andy 我可以使用哪些参数从扫描仪中查找特定字符?我尝试用存在的字母替换
".",它仍然给出NullPointerException
标签: java input nullpointerexception char