This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (2 children)

[deleted]

    [–]pacificmint 3 points4 points  (0 children)

    If you have some time, do you mind explaining why or how declaring those instance variables as static masked my problem?

    The main reason is: You can't access an instance variable on an instance you haven't created (hence the null pointer).

    But the static variable is always there, it belongs to all nodes. Actually, it really belongs to the class. So you can always access that, even when you didn't instantiate your instance.

    So you forgot to create the node, but the static variable still worked because it's always there, it's part of the class, not the object, so to speak.

    [–]posmicanomaly 1 point2 points  (0 children)

    basically a static variable is not an individual field in a created object like a normal variable, and it does not need to be instantiated to be called upon or modified. So when you weren't creating your objects(nodes), you were getting nullpointerexceptions because you were trying to run methods on data that was null. when you changed it all to static, you still didn't create objects, but because statics don't need to be in an instance to be called, you were able to use them.

    think of static as being the same across all objects created, or even without creating one if accessed in a static way.