I am currently using blueJ to learn java and I have an assignment where I have to write to a txt file, check if the file exists and read the file. The code I have is below which compiles but when I try to run the write() method,I receive the follow error java.lang.nullpointerexception;
I do not know where I am going wrong and it is beginning to drive me nuts at this stage.
import java.io.*;
public class ReadWrite
{
// instance variables - replace the example below with your own
private String file;
private String text;
/**
 * Constructor for objects of class ReadWrite
 */
public ReadWrite(String file, String text)
{
    // initialise instance variables
    file=this.file;
    text=this.text;
}
public void write() 
{
    try{
        FileWriter writer = new FileWriter(file);
        writer.write(text);
        writer.write('\n');
        writer.close();
    }
    catch(IOException e)
    {
        System.out.print(e);
    }
}
public boolean writeToFile()
{
    boolean ok;
    try{
        FileWriter writer = new FileWriter(file);
        {
          write();
        }
        ok=true;
    }
    catch(IOException e) {
        ok=false;
    }
    return ok;
    }
public void read(String fileToRead)
{
    try {
         BufferedReader reader = new BufferedReader(new        FileReader(fileToRead));
         String line = reader.readLine();
           while(line != null) {
               System.out.println(line);
               line = reader.readLine();
            }
            reader.close();
                }
                catch(FileNotFoundException e) {
                }  
                catch(IOException e) {
                } 
}
}
 
    