0

I want to create a play() method with Track in the method parameters but I can't do this in my controller class as the play is an action and actions can't have parameters (or it must be an ActionEvent).

So I thought of creating another class to apply play logic instead and just pass to this method in the new class instead of doing everything there.

@FXML
private void play() {
    play.setText("Pause");
    Track selectedTrack = playingTable.getSelectionModel().getSelectedItem();

    if (selectedTrack != null && selectedTrack != playingTrack) {
        player.stopSong();
        player = new TrackPlayer(selectedTrack);
        playingTrack = selectedTrack;
        player.playSong();
    } else {
        switch (player.getStatus()) {
            case READY:
                player.playSong();
                break;
            case PLAYING:
                player.pauseSong();
                play.setText("Play");
                break;
            case PAUSED:
                player.playSong();
                play.setText("Pause");
                break;
            default:
                System.out.println("Wtf?");
        }
    }
    updateInfo();
}

This is my play() method now inside my controller class (which is working correctly). I want to change it so I can do the following

if(selectedTrack != null) {
    manager.play(selectedTrack);
} else {
    System.out.println("Track not selected / null");
}

manager being another class which would have the following method

public void play(Track track) {
    controller.setPlayTxt("Pause");

    if (track != null && track != playingTrack) {
        player.stopSong();
        player = new TrackPlayer(track);
        playingTrack = track;
        player.playSong();
    } else {
        switch (player.getStatus()) {
            case READY:
                player.playSong();
                break;
            case PLAYING:
                player.pauseSong();
                controller.setPlayTxt("Play");
                break;
            case PAUSED:
                player.playSong();
                controller.setPlayTxt("Pause");
                break;
            default:
                System.out.println("Wtf?");
        }
    }
}

However whenever I click on a song (that is not null) and press play I get a NullPointerException on the line manager.play(selectedTrack);

Why? I know that the object is not null, and have checks for it, and this is the exact same way I do it originally (which works) but when I change this to be a pass to another class I get NullPointerException.

1 Answers1

0

"manager" is null, not "selectedTrack". The code you have given isn't enough to say why "manager" is null, but that's what the problem is. Please post more code if you need more help.

Where do you assign to "manager" before the code that errors runs?

From the below comments, it sounds like the asker had not assigned to the "manager" field before the code that throws ran. The solution is to assign a TrackPlayerManager object to the "manager" field.

Aurast
  • 3,189
  • 15
  • 24