我在playlist1.firstSong = song;
下面的行(第9行)中收到nullPointerException错误。有任何想法吗?
播放清单类别:
public class Playlist {
Scanner console = new Scanner(System.in);
private Playlist playlist1=null, playlist2=null;
private Song firstSong;
private Song secondSong;
private Song thirdSong;
public void setSong(Song song) {
if (song != null) {
if (playlist1.firstSong == null) {
playlist1.firstSong = song;
System.out.println("The song has been added to the playlist.");
}
else if (playlist1.secondSong == null) {
playlist1.secondSong = song;
System.out.println("The song has been added to the playlist.");
}
else if (playlist1.thirdSong == null) {
playlist1.thirdSong = song;
System.out.println("The song has been added to the playlist.");
}
else {
System.out.println("This playlist is currently full with 3 songs. Please delete a song before attempting to add a new one.");
}
}
}
addSongToPlaylist方法:
private void addSongToPlaylist() {
if (songCount <=3) {
System.out.println("Please enter the number of the song you'd like to be added to the playlist.");
System.out.println("");
database.Display();
int songNumber;
songNumber = console.nextInt();
switch (songNumber) {
case 1:
playlist.setSong(database.getSong(1));
break;
case 2:
playlist.setSong(database.getSong(2));
break;
case 3:
playlist.setSong(database.getSong(3));
break;
case 4:
playlist.setSong(database.getSong(4));
break;
default:
System.out.println("Please enter a valid song number.");
break;
}
songCount++;
}
getSong方法:
public Song getSong(int songNumber) {
if (songNumber == 1){
return song1;
}
else if (songNumber == 2){
return song2;
}
else if (songNumber == 3){
return song3;
}
else if (songNumber == 4){ /
return song4;
}
else {
return song1;
}
}
任何帮助将不胜感激,谢谢!
嗯...您在播放列表课程中感到困惑。您的播放列表实际上存储了3首歌曲,仅此而已,您无需使用playlist1和playlist2。尝试改用这个:
public class Playlist {
private Song firstSong;
private Song secondSong;
private Song thirdSong;
public void setSong(Song song) {
if (song != null) {
if (this.firstSong == null) {
this.firstSong = song;
System.out.println("The song has been added to the playlist.");
}
else if (this.secondSong == null) {
this.secondSong = song;
System.out.println("The song has been added to the playlist.");
}
else if (this.thirdSong == null) {
this.thirdSong = song;
System.out.println("The song has been added to the playlist.");
}
else {
System.out.println("This playlist is currently full with 3 songs. Please delete a song before attempting to add a new one.");
}
}
}
想想当您创建播放列表实例时,可以在其中存储3首歌曲,仅此而已。如果您需要更多播放列表,则可以创建更多播放列表。
但是,如果您要存储3首以上的歌曲怎么办?为此,您可以仅使用array
例如(或任何类型的存储)重构代码。让我们尝试一个Vector:
public class Playlist {
private Vector<Song> songList;
public void setSong(Song song) {
if (song != null) {
songList.add(song);
}
}
public Song getSong(int nb) {
if (nb > 0 && nb < songList.size()) //We don't want to check the song #-1 or a song that would be out of bonds
return songList.elementAT(nb);
}
}
而且那里还有一些更清洁的东西。(上面的代码中肯定有一些错别字,我在这里不能真正检查它们,但这只是一个例子。)
作为评论的答案:如果您想使用2个播放列表,也可以,只需在您的main中使用:
Playlist firstPlaylist;
Playlist secondPlaylist;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句