user690153
user690153

Reputation: 3

Nullpointer exception when instantiating an array

I get a java.lang.NullPointerException when I run the code below:

import org.apache.axis.types.*; //to use UnsignedShort

UnsignedShort a = new UnsignedShort(1);
UnsignedShort b = new UnsignedShort(1);
int c= 123;

VARDATA[] data = new VARDATA[1];

data[0] = new VARDATA();
data[0].setUsType(a);
data[0].setUsIndex(b);
data[0].setUlValue(c);

Upvotes: 0

Views: 128

Answers (1)

Ben
Ben

Reputation: 7597

Where do you get the NullPointerException? Which line? Re VARDATA, I don't see it imported, so presume it's part of the same package as the code you're running.

For readability I would prefer to set the first element of data explicitly, and then assign it to the array reference, i.e. something like this:

VARDATA[] data = new VARDATA[1];

VARDATA d = new VARDATA();
d.setUsType(a);
d.setUsIndex(b);
d.setUlValue(c);

data[0] = d;
// And so on ...

… but that's up to you. Either way, I think you need to post VARDATA as I suspect that is where the issue lies here.

Upvotes: 1

Related Questions