I have an array of objects but when i traverse throught it to display its contents, it gives an error, but only if it is not full. Any suggestions? Below is the code snippet:

for (int k = 0; k < array.length; k++)
        {
            msg += String.format(array[k].toString());
        }

JOptionPane.showMessageDialog(null, msg);

Recommended Answers

All 15 Replies

it gives an error,

Please post the full text of the error message.

if it is not full.

Where are the empty spots in the array? If at the end, change the for statement to not go past where there is good data.

Error: NullPointerException
Empty spots are at the end of the array

Do you understand how to change the for loop statement to keep away from the null elements at the end of the array?

Either you can use an if statement to check if the element you are retrieving is null or you have to keep a record of the size of the populated array for your condition of the loop instead of the size of the array.

do you understand what null is? it's the default value for a non-instantiated but declared Object.
since it has no value (yet) you can't call a method on it.
there are two ways to avoid this from happening:
1. add an conditional statement, checking whether or not the element you're trying to work with is null or not (as been suggested earlier)
2. make sure your array doesn't contain any null values.
for instance:

String[] myArrray = new String[5];
for ( int i = 0; i < myArray.length; i++) 
  myArray[i] = new String("");

off course, this is just a very basic example, but even though all the String-elements of myArray are an empty String (and you can decide on what your default value could/should be in your case), none of them will cause a null pointer exception.

How do make it stay away from null values?

if ( element != null )

or instantiate all the elements with a default value.

I did this but it's still not working:

for (int k = 0; k < array.length; k++)
        {
            if (array[k].getElement() != null)
                msg += String.format(array[k].toString());
            else
                continue;   
        }

because you are calling the getElement() method on your element, which is null. Also, since there is no code in your loop after the continue statement, you don't need it. try this:

for ( int k = 0; k < array.length; k++)
{
  if ( array[k] != null)
    msg += String.format(array[k].toString());
}

It's still giving the same error

can you show your current code along with the complete stacktrace? and also add which line is which, that we know the number of the line we have to look at.

This is basically the code I'm having a problem with, including the bubble sort first:

for (int k = 0; k < array.length; k++)
        {
            for (int j = 0; j < (array.length - 1 - k); j++)
            {
                if (array[j].getTitle().compareTo(array[j+1].getTitle()) > 0)
                {
                    tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                }
            }
        }

        for (int k = 0; k < array.length; k++)
        {   
            if (array[k].getTitle() != null)
                msg += String.format(array[k].toString());
        }

        JOptionPane.showMessageDialog(null, msg);

@cross213:
stultuske is giving you good advice, so you should make more effort to follow it rather than wasting his time. Your latest code completely fails to implement the perfectly good solution that he gave you earlier. Re-read his posts and think carefully about what he says..

null.getTitle() is what you are doing. you are testing on the wrong element, as I've said before.

It works now yes thanks, i get it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.