Null pointer with array


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Null pointer with array

  1. #1
    Join Date
    Oct 2004
    Posts
    2

    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???

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    show us your code you're using.

  3. #3
    Join Date
    Sep 2004
    Posts
    223
    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

  4. #4
    Join Date
    Oct 2004
    Posts
    2
    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.

  5. #5
    Join Date
    Sep 2004
    Posts
    223
    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

  6. #6
    Join Date
    Feb 2004
    Posts
    541
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center