1

Here is my Controller Class:

package sample;

import javafx.fxml.FXML;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView; 

public class Controller {

    @FXML
    private MediaView mediaView;

    private Media mVideo;
    private MediaPlayer mPlayer ;



    public Controller(){
        karaokePlayer();
    }

    public void karaokePlayer(){
        String path = getClass().getResource("/video/TestVideo.mp4").toExternalForm();
        mVideo = new Media(path);
        mPlayer = new MediaPlayer(mVideo);
        mPlayer.setAutoPlay(true);
        mediaView.setMediaPlayer(mPlayer);
    }
}

Here is my FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.media.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>


<AnchorPane fx:controller="sample.Controller" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <BorderPane layoutX="38.0" layoutY="39.0" prefHeight="322.0" prefWidth="524.0">
         <center>
            <MediaView fx:id="mediaView" fitHeight="200.0" fitWidth="200.0" BorderPane.alignment="CENTER" />
         </center>
      </BorderPane>
   </children>
</AnchorPane>

My problem is that i am getting a null pointer exception at this line: mediaView.setMediaPlayer(mPlayer); However looking at my code i do not understand why the null pointer exception. I have set this up by writing out all the GUI code manually and it seems to work just fine. I have looked at several documents for java FXML on line and cannot quite understand why i would be getting a null pointer in this instance. My question is why the null pointer exception? The media player is created and is not null yet when i call the .setMediaPlayer() method the compiler comes back as though it is null.

  • So based on this, im getting a null pointer exception because i am not using the public void initialize() { karaokePlayer(); } Thank you, sorry for duplicate post – bucknerbg27 Feb 10 '16 at 20:13
  • Yes, exactly. The linked question explains why too. No problem about a duplicate, they actually help to direct users to useful answers. – James_D Feb 10 '16 at 22:44

0 Answers0