This is a simple program to read from a text file, but somehow I am getting the following error when i run it
Exception in thread "main" java.lang.NullPointerException
Program:
package testenglish;
import java.util.*;
import java.io.*;
public class TestEnglish
{
public static void main(String[] args)
{
Scanner myscanner= null;
try
{
myscanner = new Scanner(new File("file.txt"));
}
catch (FileNotFoundException e)
{
}
while (myscanner.hasNextLine())
{
}
myscanner.close();
}
}
I don't understand why I am getting a NullPointerException
when myscanner
has already been initialized.
EDIT: How come I am not reading the text file? It is placed in the same folder as the source code.