我正在尝试用Java中的scanner读取文件。当我运行下面这段代码时,我得到了以下输出和错误。
Exception in thread "main" java.lang.NullPointerException at PageRank$ReadInput.ReadFile(PageRank.java:29) at PageRank.main(PageRank.java:58)
请帮我修复这个错误。
public class PageRank{
public static class ReadInput{
private Scanner x;
public void OpenFile(){
try {
File file = new File("input.txt");
Scanner x=new Scanner(file);
}
catch(Exception e){
System.out.println("File does not exist.");
}
}
public void ReadFile() {
while (x.hasNextLine() ) {
String s = x.nextLine();
System.out.println(s);
String s1 = x.nextLine();
String s2 = x.nextLine();
System.out.println(s);
System.out.println(s1);
System.out.println(s2);
}
}
}
public static void main(String[] args)throws Exception
{
ReadInput P = new ReadInput();
P.OpenFile();
P.ReadFile();
}
}
https://stackoverflow.com/questions/50788557
复制相似问题