Please help! I'm new to Java and have done the following:
Created an array to hold 100 records.
Created 5 records for the array (index 0, 1, 2, 3, 4)
Now I'm trying to use a for loop to have a write output of the array values and I'm getting a NULL pointer exception.
Any ideas???
10-10-2004, 03:41 PM
Phaelax
show us your code you're using.
10-10-2004, 09:31 PM
Kram
ill assume that each object in the array is in fact an object rather than a primative data type, if you use a for loop to go through each one and you only have filled up 5 out of 100 then the 6th one will be null, you have to do a check on each array object like so:
Code:
for(int count=0;count<=array.length();count++){
if(array[count] != null){
//do you thing here
}
}
this will check for null on each item in the array
10-10-2004, 10:35 PM
kgibson80
Thanks Kram, that worked!
I was under the impression that those after the first five (that I did not set) were assigned the value from the default constructor of my object.
10-10-2004, 11:23 PM
Kram
cool glad to help, i often forget to do null pointer checking, but when working with object oriented programming, its a must do :)
10-11-2004, 06:44 AM
mikeBarr81
Only primitives get set to default values. Objects all get set to null.