-3

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.

khelwood
  • 55,782
  • 14
  • 81
  • 108
user3531649
  • 63
  • 1
  • 1
  • 6

1 Answers1

2

You are never assigning anything to o other than null. You need to change this line: Open_File o = null; to Open_File o = new Open_File();.

npinti
  • 51,780
  • 5
  • 72
  • 96