I'm trying to read a txt file using this code.
This is my function which reads the file:
public class Open_File {
public void fct(File file) throws IOException {
String line = null;
FileReader fr = null;
{
try
{
fr = new FileReader( file );
}
catch (FileNotFoundException e)
{
System.out.println( "File doesn't exists" );
e.printStackTrace();
}
BufferedReader br = new BufferedReader( fr );
try
{
while( (line = br.readLine()) != null )
{
System.out.println( line );
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
br.close();
}
}
}
}
And this is my main function, where I give the path:
public static void main(String[] args) throws IOException {
Open_File o = null;
File file= new File("C:/test.txt");
o.fct(file);
System.out.println("réussi");
}
The problem is that the execution throws a NullPointerException
Exception in thread "main" java.lang.NullPointerException
at test.main(test.java:17)
Please could anyone give me a solution for this problem.