0

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);
3
  • 1
    Please add the stacktrace or the exact location where the exception is thrown from Commented Apr 4, 2011 at 8:05
  • 1
    Please show the full code and not just those lines where you can't find the problem. Commented Apr 4, 2011 at 8:10
  • And add also a little bit more code. For example part of the UnsignedShort or VARDAT class that is throwing the exception. Commented Apr 4, 2011 at 8:10

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Hello Ben, A slight modification to your code is working perfectly. VARDATA data0 = new VARDATA(); data0.setUsType(varType); data0.setUsIndex(varIndex); data0.setUlValue(varValue); VARDATA[] data = { data0 }; When I define VADATA[] data = new VARDATA[1]; I got error if I assign data[0] = something, the above modified version did the trick. Thanks ... PS: My comments appear in a straight line, how can I format them? This is my first post.
@user690153 - we can't do a formatted multiline comment, but you can format parts of the comment as code - I usually append info like that to my original question.
@user690153 Glad to hear you got it sorted. Re comments as Andreas_D says, you can only do inline code in comments, using the ` character around the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.