2015-05-04 25 views
3

我在下面的行(第9行)playlist1.firstSong = song;行中收到nullPointerException錯誤。有任何想法嗎?Instanciate類中的類NullPointerException

Playlist類:

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

嗯......你在你的播放列表類糊塗了。您的播放列表實際上存儲了3首歌曲,就是這樣,您不需要使用播放列表1 & 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播放列表,那很好,只是在你的主要使用:

Playlist firstPlaylist; 
    Playlist secondPlaylist; 
+3

@StephenC我正在上傳代碼,等一下。對於初學者,我想表明這比繼續使用playlist1和playlist2更合適。 – Mekap

+0

我需要有2個播放列表,用戶可以將歌曲存入。所以我不需要playlist1和playlist2對象? – Lachie

+1

@Lachie你可以有2個同一個類的實例! (這是「公安條例」的權力);我編輯了我的代碼,以便看到我在說什麼。 – Mekap

7

因爲您沒有初始化播放列表對象。做到以下幾點,

做到這一點。

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. 



} 
} 

我希望你明白我的意思

+0

當然,謝謝! – Lachie

+1

我發現這個解決方案很懶,因爲你解決了一個XY問題的X – Mekap

+0

@Mekap我不認爲它很懶,他們回答了OP的具體問題。也許還有其他錯誤,但是這個答案解決了這個問題。 – AdamMc331

0

我找不到你在哪裏初始化播放列表對象。您將它設置爲空,但不會初始化它。

無論如何,最好檢查playList對象是否爲null,然後設置字段的值(firstSong)。