Exclude class inside class NullPointerException

I am getting nullPointerException error on the line playlist1.firstSong = song;

below (9th line). Any ideas?

Playlist 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 method:

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 method:

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

      

Any help would be really appreciated, thanks!

+3


source to share


3 answers


Hmm ... you are confused about your Playlist class. Your playlist actually stores 3 songs and that's it , you don't need to use playlist1 and playlist2. Try using this instead:

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.");
        }
    }
   }

      

Think that when you instantiate a playlist, you can store 3 songs in there and that's it. If you want more playlists, you can create more.

But what if you want to store more than three songs? To do this, you can simply refactor your code using array

for example (or any repository, really.). Try with 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);
}
 } 

      

And you have something cleaner. (There are some typos in the code above, I can't check them here, but it serves as an example.)

As a response to a comment: If you want to use 2 playlists that's fine, just use in your main one:

   Playlist firstPlaylist;
   Playlist secondPlaylist;

      

+3


source


because you haven't initialized the playlist object. Do the following,

Do it.

   private Playlist playlist1= new PlayList(), playlist2=new Playlist(); 

      

instead

   private Playlist playlist1=null, playlist2=null; 

      

EDIT:



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

}

      

The way you were doing the code is wrong because.

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

      

When to try to access the song you get NullPointerexception again

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. 



}
}

      

Hope you get my point.

+7


source


I cannot find where you are initializing the Playlist object. You set it to null, but you never initialize it.

In any case, it's better to check if the playList object is null and then set a value for the (firstSong) field.

0


source







All Articles