简体   繁体   English

Javafx mediaViewer / mediaPlayer引发java.lang.reflect.InvocationTargetException和java.lang.NullPointerException

[英]Javafx mediaViewer/mediaPlayer throwing java.lang.reflect.InvocationTargetException and java.lang.NullPointerException

So I have this code here: 所以我在这里有这段代码:

package sample;

import javafx.beans.binding.Bindings;
import javafx.beans.property.DoubleProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.FileChooser;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {

    private MediaPlayer mediaPlayer;

    @FXML
    private MediaView mediaView;

    private String filePath;

    @FXML
    private void handleButtonAction (ActionEvent event){
        FileChooser fileChooser = new FileChooser();
        FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Select a File (*.mp4)", "*.mp4");
        fileChooser.getExtensionFilters().add(filter);
        File file = fileChooser.showOpenDialog(null);
        filePath = file.toURI().toString();

        if(filePath != null) {
            Media media = new Media(filePath);
            mediaPlayer = new MediaPlayer(media);
            mediaView.setMediaPlayer(mediaPlayer);
            DoubleProperty width = mediaView.fitWidthProperty();
            DoubleProperty hight = mediaView.fitHeightProperty();

            width.bind(Bindings.selectDouble(mediaView.sceneProperty(), "width"));
            hight.bind(Bindings.selectDouble(mediaView.sceneProperty(), "hight"));

            mediaPlayer.play();
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }
}

And you can run it. 您可以运行它。 But if you try to open a file using the openfile button, the video wont appear and I just get a list of exceptions. 但是,如果您尝试使用openfile按钮打开文件,则视频将不会出现,而我仅获得例外列表。 I'dont know if it's just me or if I did something wrong. 我不知道这只是我还是我做错了什么。

The NullPointerExeption is in line 38, trusting IntelliJ wich I am using. NullPointerExeption位于第38行,信任我正在使用的IntelliJ。

Caused by: java.lang.NullPointerException
 at sample.Controller.handleButtonAction(Controller.java:38))

So is it something with the mediaPlayer/mediaViewer then? 那么mediaPlayer / mediaViewer可以解决问题吗? (mediaView.setMediaPlayer(mediaPlayer);) (mediaView.setMediaPlayer(MEDIAPLAYER))

所以我忘了做这行(或通过SceneBuilder添加)

<MediaView fx:id="mediaView" fitHeight="200.0" fitWidth="200.0" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaFX MediaPlayer抛出java.lang.reflect.InvocationTargetException - JavaFX MediaPlayer throws java.lang.reflect.InvocationTargetException java.lang.reflect.InvocationTargetException JavaFX TableView - java.lang.reflect.InvocationTargetException JavaFX TableView MockWebServer抛出java.lang.reflect.InvocationTargetException - MockWebServer throwing java.lang.reflect.InvocationTargetException javafx中的java.lang.reflect.InvocationTargetException中的异常 - Exception in java.lang.reflect.InvocationTargetException in javafx javaFx中的java.lang.reflect.InvocationTargetException - java.lang.reflect.InvocationTargetException in javaFx java.lang.reflect.InvocationTargetException - java.lang.reflect.InvocationTargetException java.lang.reflect.InvocationTargetException? - java.lang.reflect.InvocationTargetException? 如何在JavaFX中交换控制器? java.lang.reflect.InvocationTargetException - How to swap controllers in JavaFX? java.lang.reflect.InvocationTargetException JavaFX:应用程序启动方法中的异常 java.lang.reflect.InvocationTargetException - JavaFX: Exception in Application Start Method java.lang.reflect.InvocationTargetException 启动教科书文件时的JavaFX java.lang.reflect.InvocationTargetException - JavaFX java.lang.reflect.InvocationTargetException when launching textbook file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM