Hi All,

I have an array of 3 objects stored in cdList.

Because I have now added them to an array, the usual method of displaying no longer works:

System.out.println(cd1.getArtist() + " " + cd1.getTitle() + " " + cd1.getCost());

My questions are......

how would i amend to output the array contents?

my g

I would like to run a loop to run through the array and i THINK my attempt just needs a slight change becuase

System.out.println(cdList[1]);

works,but as a newbie im struggling a little bit...

for (int i = 0; i < cdList.length; i++)
      System.out.println(cdList[i]);

Thanks again

Hi All,

I have an array of 3 objects stored in cdList.

Because I have now added them to an array, the usual method of displaying no longer works:

System.out.println(cd1.getArtist() + " " + cd1.getTitle() + " " + cd1.getCost());

My questions are......

how would i amend to output the array contents?

my g

I would like to run a loop to run through the array and i THINK my attempt just needs a slight change becuase

System.out.println(cdList[1]);

works,but as a newbie im struggling a little bit...

for (int i = 0; i < cdList.length; i++)
      System.out.println(cdList[i]);

Thanks again

You can combine the top code and the bottom code and get...

for (int i = 0; i < cdList.length; i++)
      System.out.println(cdList[i].getArtist() + " " + cdList[i].getTitle() + " " + cdList[i].getCost());

You say this below worked? Really? That surprises me. Does it look nice? Regardless, try what's above.

System.out.println(cdList[1]);

CD details: morning glory By: Oasis Price:3.99 is what is displayed by using

System.out.println(cdList[1]);

the amended code displays:
CD details: morning glory By: Oasis Price:3.99
Kaiser up the khazi 9.99
Exception in thread "main" java.lang.NullPointerException
Oasis morning glory 3.99
Bob Dylan Alreet Sunna 6.99
at weektwohomework.CDDriver.main(CDDriver.java:33)

What is wierd, is there are no errors using netbeans. Thanks for your help

Keep in mind your array elements ("slots") are null until you put something in them. If you declare the array to have 10 and only fill three then the remaining seven will throw errors if you attempt to read data from them - hence a "null pointer" error.

excellent - thank you so much for that, works a treat so many thanks!!!

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.