Click to See Complete Forum and Search --> : nullpointerexception problem
attilio
April 15th, 2006, 07:02 PM
hi,
Im having problems with nullpointerexception! Nothing I seem to do works.
I have a lot of arrays of which are created but not populated until a certain stage thus nullpointer on unpopulated arrays occurs.
is there something I can check the String[][] array for before it causes an error? I have tried checking if array[n][0]!=null before I continue but that seems to be ignored.. can anyone help me out?
Perforated
April 15th, 2006, 10:03 PM
I think that your problem is that you are checking whether specific locations of the array is null or not when you really wantto check if the reference to the array is null.
try writing this instead:
if (nameOfArray != null)
{
whatever code you want.
}
I believe that should solve your problem.
gmrowe1075
April 16th, 2006, 07:36 AM
I have tried checking if array[n][0]!=null before I continue but that seems to be ignored..
What do you mean when you say it seems to be ignored? If the array hasn't been populated then certainly that element should be null.
Perforated
April 16th, 2006, 09:58 AM
I believe he hasn't instantiated the array so the reference to it is null. Trying to access an element in the array would produce a NullPointerException in that case.
attilio
April 16th, 2006, 10:42 AM
hi thanks for the responses...
I dont understand what you mean by nameOfArray?? are you talking about a direct reference to the specific element of the array?
ie if(array[n][0]!=null) {
System.out.println(array[n][0]);
}
because that is what I am doing, and it is ignored - ie the error would occur on the 2nd line: System.out.println(array[n][0]);
gmrowe1075
April 16th, 2006, 02:27 PM
...because that is what I am doing, and it is ignored - ie the error would occur on the 2nd line: System.out.println(array[n][0]);
Well, that line shouldn't produce a NPE even if the element is null, it would simply print null. There is no reason the test should be being ignored however. Could you show a couple lines of the actual code?
Perforated
April 16th, 2006, 03:15 PM
hi thanks for the responses...
I dont understand what you mean by nameOfArray?? are you talking about a direct reference to the specific element of the array?
ie if(array[n][0]!=null) {
System.out.println(array[n][0]);
}
because that is what I am doing, and it is ignored - ie the error would occur on the 2nd line: System.out.println(array[n][0]);
I'm sorry if I wasn't clear the first time around, let's see if I can rectify that.
What I meant with nameOfArray is the reference to the memory location where the array is stored, this would be the "variable" name i.e.
char[] foo;
is an array of chars with the name foo. If you try to reference this name or any element in it and the array has not been initialized (new) it will produce a NullPointerException.
However, if you do like this:
char[] foo = new char[10];
and then:
System.out.println(foo[i]);
and the element is empty it will print null on the screen.
Is this a better explanation?
attilio
April 16th, 2006, 03:54 PM
yes i understand - and i did before I guess but what Im doing is actually creating the array correctly like you have done:
static String[][] array = new String[100][2];
then trying to access unpopulated areas of that >> let me rectify.
Im cycling the array using a for loop until array[n][0] == null >> at which point it would break heres my code:
for(int k =0; k<100; k++) {
if(mainThread.array[k][0]!=null) {
int id = Integer.parseInt(mainThread.array[k][0]);
//etc
//
//
//
} else {
k = 100;
break;
}
}
from what your saying this shouldn't have a problem though?
Perforated
April 16th, 2006, 07:37 PM
You are quite correct, from what I'm able to see you should not have that problem. So I'm starting to think that your problem is elsewhere. Hard to tell without being able to see the code though.
attilio
April 17th, 2006, 09:14 PM
ok thankyou, I don't know what is happening then, I am getting a nullPointerException from the following:
//head of mainThread
static String[][] array = new String[100][2];
//
//a dynamically created thread
for (int k=0; k<100; k++) {
if(mainThread.array[k][0]!=null) { //nullPointerException!
is it actually possible for me to be getting a nullPointerException from this line: if(mainThread.array[k][0]!=null) { ??
gmrowe1075
April 18th, 2006, 04:33 AM
is it actually possible for me to be getting a nullPointerException from this line: if(mainThread.array[k][0]!=null) { ??
if mainThread is null, then this code will fail with an NPE. Maybe you could insert some debugging statements in there to see if this is the case.
attilio
April 18th, 2006, 10:48 AM
mainThread cannot be null tho? its the only thread ensuring the child thread (that is calling mainThread) is actually alive
in addition mainThread is previously called without error. I will see what debugging mainThread does.
a_c
April 18th, 2006, 02:23 PM
you should give some more information. I don't understand what mainThread is. is it a class? is it a reference? where exactly are you declaring that static array?
why don't you just provide a some code (not from the application itself, but make a simple class that reproduces the error)? is it that complicated? if you would have done that from the beginning, your problems were probably solved by now.
you just keep asking as if you don't need an answer...
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.