-2

I have a tree setup. I am trying to save it into a txt file using a BufferedWriter. It creates the file, then it runs into a null pointer exception when my program first calls the write method of the BufferedReader(line 102), so I guess its not getting initialized properly? Note: "node" is just the payload of the root, I can print the tree using the same method, so the issue is not with the node variable.

public class TreeNode {
    File file;
    BufferedWriter writer;
    FileWriter fw;

    public void save() /// save tree into txt file
    {

        try 
        {
            file = new File("save.txt");
            if(!file.exists())
            {
                file.createNewFile();
            }
            fw = new FileWriter(file);
            writer = new BufferedWriter(fw);
            print();

        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }

    }
    public void print() {
        try 
        {
            writer.write(node); //line 102
            writer.newLine();
            if (no != null) 
            {
                no.print();
                writer.newLine();
            }
            if (yes != null) 
            {
                yes.print();
                writer.newLine();
            }
            writer.flush();
            writer.close();
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }

        }
}

Error:

    Exception in thread "main" java.lang.NullPointerException
    at TreeNode.print(TreeNode.java:102)
    at TreeNode.print(TreeNode.java:106)
    at TreeNode.save(TreeNode.java:90)
    at TreeNode.playagain(TreeNode.java:59)
    at TreeNode.go(TreeNode.java:189)
    at CelebrityGame.main(CelebrityGame.java:22)
Croatoan
  • 25
  • 2
  • 5
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Rick S Feb 14 '15 at 23:04
  • It looks like you're not calling `save()` before `print()` and so `writer` has not been initialised. – Robert Bain Feb 14 '15 at 23:21
  • I have read it, together with some other questions. I debugged my code, I know my "writer" is null, but I dont understand why. In your link it says: "If you tried to debug the problem and still don't have a solution, you can post a question for more help, but make sure to include what you've tried so far. At a minimum, include the stacktrace in the question, and mark the important line numbers in the code. Also, try simplifying the code first (see SSCCE)." And thats what I did. – Croatoan Feb 14 '15 at 23:38
  • You got an exception opening the file writer and carried on as though it hadn't happened, so `writer` was still null. Poorly structured exception handling. Don't write code like this. NB you don't need the exists/createNewFile part. It's just a waste of time and space. The OS already does all that when you create the file writer. You're just doubling the processing. – user207421 Feb 15 '15 at 00:14
  • @eckes node is a string defined with a constructor. But it is not null, i can print the tree to the console with the same recursive method. I am not using "writer" anywhere else than in the code that i included here. I will try to rewrite the methods differently. – Croatoan Feb 15 '15 at 00:23
  • BTW: you dont need the crateNewFile(), FileWriter will do that automatically. – eckes Feb 15 '15 at 00:25
  • @EJP not sure if any of the writer methods throw an exception then print() will not be called, it is inside the same try. – eckes Feb 15 '15 at 00:26
  • It is not inside the same try. Creating file writer is inside a try of its own. That's the problem. – user207421 Feb 15 '15 at 00:43
  • Then you have a different code then I have, I see `writer = new BufferedWriter(fw); print();` right next to each other. – eckes Feb 15 '15 at 01:16
  • BTW: since you cannot call print() from outside save() I would not make it public but private. – eckes Feb 15 '15 at 01:20

1 Answers1

1

Your problem is, that you have more than one TreeNode instance, but only the outer one has writer filled.

You can see in your stack trace that actually the outer writer was successfuly progressing to line 106, where it calls no.print(). This is another node object. And in this object it was then failing in line 102, because there the writer was null.

It is better if you remove the writer from the fields and pass it as an argument to print() and don't store the other variables as fields (file + fw) either).

eckes
  • 10,103
  • 1
  • 59
  • 71
  • As a quick test that I am right, make "writer" static, but dont keep this as a final solution. – eckes Feb 15 '15 at 00:30
  • you are right, making it static worked. Could you please explain why? Anyways, thank you, I will rewrite my code following your suggestion. – Croatoan Feb 15 '15 at 01:33
  • The explanation is in the second paragraph. – eckes Feb 15 '15 at 01:34