-
NullPointerException
//code
public static void printdata(Object[] mpdata)
{
int m=0;
while ((mpdata[m] != null)){
for (int p=0; p < mpdata.length; p++){ //the error is here
System.out.println(mpdata[p]);
}
m++;}
}
//code
Does anybody can tell me how to correct this error
-
Perhaps some array components are not initiated. It is al depends of the method param content. What and how uses the method...
-
You check the null value only for the first element of the array. What happen if the second element is null. You get the NullPointerException. That's what happen in your code.
-
Eranga, there is m++, does it work
-
Ya, m value change accordingly. Should work.
Can you show your complete error message.
-
i think there's a problem on or while loop
for example your
mpdata.lenght = 1 (modata[0] = "test")
m = 0;
the fist loop is ok but because m = 0 and address 0 is existing in your array
then when it m = 1
address 1 is not an exiting array
-
The full error is here
Exception in thread "main" java.lang.NullPointerException
at MPD.printdata(MobilePhoneDatabase.java:122)
at MPD.main(MobilePhoneDatabase.java:167)
Process completed.
The line 122 is exactly the error resist, the for loop
The line 167 is the main method call to printdata method
-
Have you checked to be sure that the array has data? Does anything get printed ? Does it fail on the first element?
Luck,
CJSL
-
The array itself may be null. Check that mpdata is not null.
-
try printing out values to the screen whenever you use them or use a debugger. it'll help you track down how the program strays from expected behavior