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);

Edited 4 Years Ago by Cross213

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.

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.

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());
}

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

This question has already been answered. Start a new discussion instead.