-
Null pointer with array
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???
-
show us your code you're using.
-
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
A kram a day keeps the doctor......guessing
-
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.
-
cool glad to help, i often forget to do null pointer checking, but when working with object oriented programming, its a must do
A kram a day keeps the doctor......guessing
-
Only primitives get set to default values. Objects all get set to null.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|