I am trying to read a file using scanner in Java. I get the following output and error when I run the below piece of code.
Exception in thread "main" java.lang.NullPointerException at PageRank$ReadInput.ReadFile(PageRank.java:29) at PageRank.main(PageRank.java:58)
Please help me fix this error.
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();
}
}