It's been a while, but I've been working on my little program that takes info about my CD collection. I've got just a little done so far -- the code takes the information for four fields, sets them and then prints the list. (This program is by no means completed-- just the beginning stages). Here's the code I've got so far:
// Set the values for the CD class instance variables.
class CD {
private String artistName, albumTitle, recordLabel, yearOfRelease;
public void setArtistName(String artist) {
artistName=artist;
}//close method
public void setAlbum(String album) {
albumTitle=album;
}//close method
public void setRecordLabel(String label) {
recordLabel=label;
}//close method
public void setYearOfRelease(String yor) {
yearOfRelease=yor;
}//close method
public String getArtistName() {
return artistName;
}//close method
public String getAlbum() {
return albumTitle;
}//close method
public String getRecordLabel() {
return recordLabel;
}//close method
public String getYearOfRelease() {
return yearOfRelease;
}//close method
public void printRecord() {
System.out.println("Artist: " + getArtistName());
System.out.println("Album Title: "+getAlbum());
System.out.println("Record Label: "+getRecordLabel());
System.out.println("Year of Release: "+getYearOfRelease());
System.out.println();
}//close method;
}//close class
import java.util.*;
import java.io.*;
import java.lang.String.*;
public class CDInputTestDrive {
public static void main (String args[]) {
String artist="";
String album="";
String label="";
String yor="";
ArrayList<CD> CDRecord = new ArrayList<CD>();
CDInputHelper helper=new CDInputHelper();
while ( ((!artist.equalsIgnoreCase("F")))
|| ((!album.equalsIgnoreCase("F")))
|| ((!label.equalsIgnoreCase("F")))
|| ((!yor.equalsIgnoreCase("F")))
) {
CD Entry = new CD();//creates a new object for the CDRecord Array List
artist = helper.getUserInput("Artist: ");//ask for artist
if (artist.equalsIgnoreCase("F")){
CDRecord.remove(Entry);
}
Entry.setArtistName(artist);
album=helper.getUserInput("Album: ");//ask for album title
if (album.equalsIgnoreCase("F")) {
CDRecord.remove(Entry);
break;
}
Entry.setAlbum(album);
label=helper.getUserInput("Record Label: ");//ask for record label
if (label.equalsIgnoreCase("F")) {
CDRecord.remove(Entry);
break;
}
Entry.setRecordLabel(label);
yor=helper.getUserInput("Year Of Release: ");//ask for year of release
if (yor.equalsIgnoreCase("F")) {
CDRecord.remove(Entry);
break;
}
Entry.setYearOfRelease(yor);
CDRecord.add(Entry);//adds entry to array list CDRecord;
System.out.println();
}//close while loop
System.out.println("Size of Array List: " +CDRecord.size()); //prints size of array
System.out.println();
for (int i=0; i<CDRecord.size(); i++) {
CDRecord.get(i).printRecord();
}
System.out.println();
}//close main
}//close class
The program works fine as long as I enter something for each field. But when I tried to just press return for a field in the test drive class (indicating that I have no info for it), I get a "Null pointer exception" which refers to the line that tests for the value of the field (the 'if' statement). I'm not sure what's going on. I want to be able to just press return if there is no info to enter, but I can't figure out how to do it (I also thought pressing return gives the 'empty string', which is not null, if I'm not mistaken). I'm trying to deciper the API docs, but I haven't the faintest idea where to look in them.