Tulula Mcabe
Tulula Mcabe

Reputation: 35

Java - MVC patterns -- Paint and Overcoming a Null Pointer Exception

Trying to implement an MVC pattern, keep coming back to a NullPointerException.

I feel I'm missing a trick when it comes to setting up an area of the GUI which should replot data based on user inputs. The exception points to this;

display.replot(model.getData(), model.getSamples());

display is an instance of a class which contains a paint method. getData and getSamples are described in the model and are based on setters which should update with user input to the GUI.

I thought the problem may have been with data set originally being null so i provided initial conditions to the model.

Upvotes: 2

Views: 258

Answers (2)

Attila
Attila

Reputation: 28762

A NullPointerException means that either you are calling a function on a non-existing (null) object, or a function call can throw that exception deliberately if one of its parameters is null. You need to check which is the case and either put an if guard around it or otherwise ensure the objects are valid.

Upvotes: 2

duffymo
duffymo

Reputation: 308813

NPE is easy to figure out with a capable IDE (e.g. IntelliJ). A pass through with a debugger will make it plain as day.

Even without one, NPE is easy to figure out because the stack trace tells you the .java source file and the line number at which it occurred. You should be able to open the .java source file, turn on line number display, and go right to the origin of the error.

Look at all the object references on that line and see which one is null. That's the one you failed to initialize.

In your case, I don't see where newPlot is initialized. You simply declare a name and reference type, but you don't ask it to point to anything. The default value is null.

You'll have the same problem if the newData List passed into the constructor is null. You don't check it.

Upvotes: 1

Related Questions