Ok wrote this program out, compile it it compiles, when i run it it starts off nicely, but when i enter Album Name it gives me an error. Neone know where im going wrong.....

import javax.swing.*;



public class CdStorage
{
public static void main (String[] args)


{
String menu_choice;
int menu;


menu_choice = JOptionPane.showInputDialog("Enter 1 to enter a new CD entry");
menu = Integer.parseInt(menu_choice);


CdRecord[] array = new CdRecord[5];
while (menu == 1)


{


array[0] . artist_name = JOptionPane.showInputDialog("Enter Album Name");


//System.out.println ("array[0]");
array[0].printCdRecord();
}
}//end main


} // end class CdStorage


class CdRecord
{
public String artist_name;
public String album_name;
public int no_of_tracks;



public CdRecord (String artist, String album, int tracks, int year)


{


artist_name = artist; //stores first argument passed into artist_name
album_name = album; //stores second argument passed into album_name
no_of_tracks = tracks; //stores third argument passed into no_of_tracks


}


public CdRecord()


{
artist_name = "A";
album_name = "B";
no_of_tracks = 0;
}


public void printCdRecord ()
{
String o = "Artist Name: " + artist_name + "\nAlbum Name: " +album_name+"\nNo. Of Tracks: " + no_of_tracks;;


System.out.println(o);
}


}//end class cdstorage

Recommended Answers

All 2 Replies

Hello,

Here is the correct program

import javax.swing.*;


public class CdStorage
{
public static void main (String[] args)

{
String menu_choice;
int menu;

menu_choice = JOptionPane.showInputDialog("Enter 1 to enter a new CD entry");
menu = Integer.parseInt(menu_choice);

CdRecord[] array = new CdRecord[5];
array[0] = new CdRecord();
while (menu == 1)

{

array[0] . artist_name = JOptionPane.showInputDialog("Enter Album Name");

//System.out.println ("array[0]");
array[0].printCdRecord();
}
}//end main

} // end class CdStorage

class CdRecord
{
public String artist_name;
public String album_name;
public int no_of_tracks;


public CdRecord (String artist, String album, int tracks, int year)

{

artist_name = artist; //stores first argument passed into artist_name
album_name = album; //stores second argument passed into album_name
no_of_tracks = tracks; //stores third argument passed into no_of_tracks

}

public CdRecord()

{
artist_name = "A";
album_name = "B";
no_of_tracks = 0;
}

public void printCdRecord ()
{
String o = "Artist Name: " + artist_name + "\nAlbum Name: " +album_name+"\nNo. Of Tracks: " + no_of_tracks;;

System.out.println(o);
}

}//end class cdstorage

Basically where you are doing wrong - recall Arrays in java are classes themselves so doing e.g. int[] a = new int[5]; initialises the array a.
Now suppose I do MyClass[] obj = new MyClass[5]; initialises the obj array. But obj[0] is still not initialized and hence if you access obj[0] you get a null pointer exception.

cheers,
aj.wh.ca

thanx mate . really apreciate the help!!!

what if the program had a menu where the user chooses to input another cd record, this data needs to be entered in a text file which i probably will be able to do,, but how will i call the other array elements in order to record the data.. the reason i need to do this is because further down my program i need to include a menu option that can delete a specific cd record without affecting the others.....???

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.