ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 在类NullPointerException中实现一个类

2019-07-17 15:02:19  阅读:186  来源: 互联网

标签:java methods nullpointerexception class


我在行playlist1.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;
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首歌曲就是这样,您不需要使用播放列表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首歌曲怎么办?为此,您可以通过使用数组(或任何类型的存储,实际上)来重构您的代码.让我们尝试使用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;

标签:java,methods,nullpointerexception,class
来源: https://codeday.me/bug/20190717/1489537.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有