Reputation: 1116
I am trying to create a constructor for a class
// Variables to send to Host
private class ParameterClass {
public String parameter;
public int value;
public Boolean sended;
}
public class SendToHostClass {
private int sizeBuffer;
public ParameterClass[] parameterList;
SendToHostClass(int sizeBufferConf) {
sizeBuffer = sizeBufferConf;
parameterList = new ParameterClass[sizeBuffer];
}
public void Put (String parameter, int valuePut, Boolean sendedPut) {
for (int index=0; index<sizeBuffer; index++) {
if (parameter == parameterList[index].parameter) {
parameterList[index].value = valuePut;
parameterList[index].sended = sendedPut;
exit();
}
}
}
}
so I declare the varible
SendToHostClass sendToHost;
and instantiate sendToHost
in setup()
Processing method using
sendToHost = new SendToHostClass(10);
sendToHost.parameterList[0].value = 0;
As I run the code, it returns me the error message
Exception in thread "Animation Thread" java.lang.NullPointerException at sketch_gui.setup(sketch_gui.java:273) at processing.core.PApplet.handleDraw(PApplet.java:2117) at processing.opengl.PGL$PGLListener.display(PGL.java:2472) at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawableHelper.java:548) at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:533) at jogamp.opengl.GLAutoDrawableBase$2.run(GLAutoDrawableBase.java:280) at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:904) at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:822) at com.jogamp.newt.opengl.GLWindow.display(GLWindow.java:543) at processing.opengl.PGL.requestDraw(PGL.java:814) at processing.opengl.PGraphicsOpenGL.requestDraw(PGraphicsOpenGL.java:1566) at processing.core.PApplet.run(PApplet.java:2020) at java.lang.Thread.run(Thread.java:662)
So what am I doing wrong?
I also discovered that if I instantiate each list object separately, it gives no error:
sendToHost = new SendToHostClass(10);
sendToHost.parameterList[0] = new ParameterClass();
sendToHost.parameterList[0].value = 0;
but it seems wrong, due to it looks that parameterList
members are being instantiated twice.
Thanks in advance.
Upvotes: 0
Views: 120
Reputation: 1500385
but it seems wrong, due to it looks that parameterList members are being instantiated twice.
You need to understand the difference between creating an array and populating it with references to objects. This:
parameterList = new ParameterClass[sizeBuffer];
... just creates an array which initially is full of null references. If you want to make the elements non-null, you have to do so explicitly... which is what you're doing in this line:
sendToHost.parameterList[0] = new ParameterClass();
You might well be better not using an array to start with though - why do you want an array? It's likely that you'd be better offer with a List<ParameterClass>
.
(As an aside, you should learn about and follow the Java naming conventions, and also keep fields private.)
Upvotes: 0
Reputation: 213223
parameterList = new ParameterClass[sizeBuffer];
The above statement is only creating an array, but not initializing the array elements. The array elements are initialized by their default value, which is null
, in case you have the array of some custom type
or some reference. So, you need to initialize the array elements separately using for loop.
So, in your constructor you need to add a loop:
SendToHostClass(int sizeBufferConf) {
sizeBuffer = sizeBufferConf;
parameterList = new ParameterClass[sizeBuffer];
for (int i = 0; i < sizeBuffer; i++) {
parameterList[i] = new ParameterList();
}
}
But of course, first you need to make your class public
, and prefer to make your fields private
instead of public
, and provide public accessors to make them accessible from outside.
Upvotes: 2