Search code examples
javabluej

Can not write to java txt file


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) {

                } 
}

}


Solution

  • Your constructor is assigning the values in reverse. At the moment you have

    public ReadWrite(String file, String text)
    {
        // initialise instance variables
        file=this.file;
        text=this.text;
    }
    

    This is assigning the incoming variables file and text to the instance variables, which are null.

    What you need to have is this:

    public ReadWrite(String file, String text)
    {
        // initialise instance variables
        this.file = file;
        this.text = text;
    }
    

    A useful way to avoid this in the future is to make your parameters final - this means you can't assign anything to them, and you'll catch this in the compiler.

    public ReadWrite(final String file, final String text)
    {
        // won't compile!
        file = this.file;
        text = this.text;
    }
    

    A further improvement would be to make the instance variables file and text final, which means they have to be assigned. This way, you're using the compiler to help you catch errors.

    public class ReadWrite
    {
        private final String file;
        private final String text;
    
        public ReadWrite(final String file, 
                         final String text)
        {
            this.file = file;
            this.text = text;
        }
    
        // ...
    }