在 class NullPointerException 中实例化 class

Instanciate a class inside a class NullPointerException

我在下面的 playlist1.firstSong = song; 行(第 9 行)中收到 nullPointerException 错误。有什么想法吗?

播放列表class:

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++; 
  }

获取歌曲方法:

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; 
}
}

非常感谢任何帮助,谢谢!

因为你没有初始化播放列表对象。执行以下操作,

这样做。

   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. 



}
}

希望你明白我的意思

嗯...您对播放列表感到困惑 class。您的播放列表实际上存储了 3 首歌曲,仅此而已,您不需要使用播放列表 1 和播放列表 2。 尝试改用它:

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;

我找不到你在哪里初始化播放列表对象。您将其设置为 null,但从未对其进行初始化。

无论如何,最好检查播放列表对象是否为空,然后为字段(firstSong)设置值。