Java 在类NullPointerException内实例化类
我在playli1.firstSong=song行中得到一个nullPointerException错误代码>在下面(第9行)。有什么想法吗 播放列表类别:Java 在类NullPointerException内实例化类,java,class,methods,nullpointerexception,Java,Class,Methods,Nullpointerexception,我在playli1.firstSong=song行中得到一个nullPointerException错误在下面(第9行)。有什么想法吗 播放列表类别: public class Playlist { Scanner console = new Scanner(System.in); private Playlist playlist1=null, playlist2=null; private Song firstSong; private Song secondSong; privat
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++;
}
非常感谢您的帮助,谢谢 因为您没有初始化播放列表对象。执行以下操作: 这样做
private Playlist playlist1= new PlayList(), playlist2=new Playlist();
而不是
private Playlist playlist1=null, playlist2=null;
编辑:
class listMenu {
// class that contains 3 playlists object.
PlayList list1 = new Playlist(), list2 = new Playlist(), list3 = new PlayList();
addPlayList() {
// whatever your logic for addition is.
}
}
你编写代码的方式是错误的,因为
class PlayList {
PlayList list1 = new PlayList(), list2 = new Playlist();
private Song song1 = null;
private Song song2 = null;
private Song song3 = null;
void setSong(Song song){
list1.song1 = song; // you are storing song in of list1.song field.
}
}
当尝试访问歌曲时,您将再次收到NullPointerexception
class Menu{
void main(// ) {
PlayList list = new PlayList();
list.setSong(new Song());
list.getSong1.name(); // throw exception, because song is stored in the member object not in itselt.
}
}
我希望你明白我的意思。。。你在播放列表课上被搞糊涂了。您的播放列表实际上存储了3首歌曲,仅此而已,您不需要使用playli1和playli2。 请尝试使用此选项:
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首以上的歌曲呢?为此,您可以使用数组来重构代码(或者任何类型的存储)。让我们试试向量:
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);
}
}
我找不到您在哪里初始化播放列表对象。您正在将其设置为null,但从未初始化它
无论如何,最好检查playList对象是否为null,然后设置字段(firstSong)的值。@Mekap我不认为这是懒惰的,他们回答了OP提出的具体问题。也许还有其他的错误,但这个答案解决了这个问题。事实上,你们指出了正确的事情,+1来自我。他也不需要播放列表对象。我没有注意到班名:p@Lachie为什么在PlayList类中需要PlayList对象?弥补点是有效的。你不应该这样做,我建议你接受化妆answer@MuneebNasir我将其更改为您发布的内容,现在得到一个java.lan.stackOverflower错误。有什么想法吗?你的答案设计有缺陷,因为你想修正错误,而不是原来的问题。@StephenC我正在上传代码,请稍等。对于初学者,我想表明这比仍然使用playli1和playli2更合适。我需要有2个播放列表,用户可以将歌曲保存到其中。那么我不需要playli1和playli2对象吗?@Lachie你可以拥有同一类的2个实例!(这就是《公安条例》的权力);我编辑了我的代码,所以你可以看到我在说什么。
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);
}
}
Playlist firstPlaylist;
Playlist secondPlaylist;