实例化我的对象,但我仍然得到一个NullPointerException错误

时间:2018-03-30 04:11:16

标签: java nullpointerexception

public class MusicPlayerAndLibraryForm extends javax.swing.JFrame {

SongPlayer SP = new SongPlayer();

int audioLength;
int audioPosition = 0;

AudioInputStream audioInputStream = null;

ArrayList<Song> songs = new ArrayList<Song>();
ArrayList<Song> songFileLibrary = new ArrayList<Song>();

String songFile;

Media media = new Media(songFile);

private MediaPlayer mediaPlayer = new MediaPlayer(media);
private Slider seekSlider = new Slider();


int currentMusicLibraryIndex = 0;
int currentSongFileIndex = 0;
public static int count;


public MusicPlayerAndLibraryForm() {
    initComponents();

}


private void jButtonPlayActionPerformed(java.awt.event.ActionEvent evt) {                                            


   String songName = jListMusicLibrary.getSelectedValue();
    songFile = jListSongFiles.getSelectedValue();



   SP.Stop();


   this.mediaPlayer.currentTimeProperty().addListener(new ChangeListener<Duration>() {
   @Override
   public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {
       MusicPlayerAndLibraryForm.this.seekSlider.setValue(newValue.toSeconds());
   }
   });

   this.seekSlider.setOnMouseClicked(new EventHandler<MouseEvent>() {
       @Override
       public void handle(MouseEvent event) {
           MusicPlayerAndLibraryForm.this.mediaPlayer.seek(Duration.seconds(MusicPlayerAndLibraryForm.this.seekSlider.getValue()));
   }
   });

   jTextFieldSongPlaying.setText(songName);
   SP.PlayBack(songFile);
}   

匪徒似乎可能是“Media media = new Media(songFile);”和“私人MediaPlayer mediaPlayer =新MediaPlayer(媒体)”系列。我不确定程序为什么会返回此错误。我相信我的所有对象都已经实例化,所以我不知道程序在哪里变为null而不是对象。

1 个答案:

答案 0 :(得分:0)

这是因为您的字符串为null并且您将其作为参数传递。它将抛出Null指针异常。

检查一下 - https://docs.oracle.com/javafx/2/api/javafx/scene/media/Media.html