0

I'm writing to a newly created text file in the constructor of a class using BufferedWriter. The class creates the text file, and then adds preliminary text to it so that read operations don't return an error. When I try to add this preliminary text, however, specifically using writeHigh in the method below, I receive a java.lang.NullPointerException. I don't think this has anything to do with the string I'm passing in, but I've also tried changing my instantiations of writeHigh, which did nothing. I was hoping someone might know what the cause of this error is. The stack trace is not helping.

    try
    {   
        //New buffered writters that can write to the specified files.
        writeHigh = new BufferedWriter(new FileWriter(HIGH_SCORE_PATH, false));

        //highScore = Integer.parseInt(readScore.readLine());
    }
    catch(FileNotFoundException e) //If reading from these files fails because they don't yet exist...
    {
        File highFile = new File(HIGH_SCORE_PATH); //Create a new high score file, this is the first time the game has been played.

       try
       {
           highFile.createNewFile();
           writeHigh.write("0000"); //This line is throwing the error
       }
       catch(IOException i)
       {
           i.printStackTrace();
       }
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
Hayden
  • 41
  • 7
  • 1
    stacktrace does not help you, but it may help us. post it please – mangusta May 29 '18 at 14:17
  • "The stack trace is not helping" -> a stack trace is always helping. In your case it's helping cause it tells you that the error appears at the line `writeHigh.write"0000");`. Then you could easily conclude that the problem is that `writeHigh` is null. See the stackTrace is useful. Moreover you should reformulate your question to "why is BufferedWriter null ?" otherwise your question is just a duplicate of https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – vincrichaud May 29 '18 at 14:23

3 Answers3

1

This line is throwing you a NPE

writeHigh.write("0000");

And you reach this line only if you've catched a FileNotFoundException. Which means that this line as thrown that exception

writeHigh = new BufferedWriter(new FileWriter(HIGH_SCORE_PATH, false));

If it has thrown an exception, it means it has failed to execute, then writeHigh hasn't been instantiated. So writeHigh is null. And so writeHigh.write("0000"); throw a NPE.


I think you wanted to do

highFile.write("0000");
vincrichaud
  • 2,218
  • 17
  • 34
0

if this fails

writeHigh = new BufferedWriter(new FileWriter(HIGH_SCORE_PATH, false));

writeHigh is later null

Michal
  • 970
  • 7
  • 11
  • What would cause that to be null in the constructor immediately after it was instantiated? – Hayden May 29 '18 at 14:18
  • new FileWriter(HIGH_SCORE_PATH, false) throws exception and your writeHigh is never initialized – Michal May 29 '18 at 14:19
0

it seems you have a "FileNotFoundException" in the first try bloc. A this point "writeHigh " is null and you not set another value before your "writeHigh.write" call

DOM67
  • 11
  • 3