简体   繁体   English

JavaFX中的FileChooser提供NullPointerException

[英]FileChooser in JavaFX giving NullPointerException

This is my class: 这是我的课:

package com.movie;

import java.io.File;

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

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.FileChooser;

public class MoviePlayerController implements Initializable {

    private BooleanProperty play = new SimpleBooleanProperty(true);

    @FXML
    private BorderPane borderPane;

    @FXML
    private MediaView mediaView;

    @FXML
    private ImageView playView;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        onStartUp();
        playView.setImage(new Image(getClass().getResource("Pause.png").toExternalForm()));
        playView.setVisible(false);
        playView.setOnMouseClicked(event -> onPlayView());
    }

    public void onStartUp() {
        try {
            FileChooser fileChooser = new FileChooser();
            File file = fileChooser.showOpenDialog(borderPane.getScene().getWindow());
            if(file != null) {
                mediaView.toFront();
                mediaView.setMediaPlayer(new MediaPlayer(new Media(file.toURI().toURL().toExternalForm())));
                mediaView.getMediaPlayer().play();
                playView.setVisible(true);
                playView.toFront();
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public void onPlayView() {
        if(play.get()) {
            playView.setImage(new Image(getClass().getResource("Resume.png").toExternalForm()));
            mediaView.getMediaPlayer().pause();
            play.set(false);
        } else {
            playView.setImage(new Image(getClass().getResource("Pause.png").toExternalForm()));
            mediaView.getMediaPlayer().play();
            play.set(true);
        }
    }
}

I'm getting this error: 我收到此错误:

java.lang.NullPointerException
    at com.movie.MoviePlayerController.onStartUp(MoviePlayerController.java:44)
    at com.movie.MoviePlayerController.initialize(MoviePlayerController.java:35)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2542)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2435)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3208)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3169)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3142)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3118)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3098)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3091)
    at com.movie.MoviePlayer.start(MoviePlayer.java:19)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
    at com.sun.javafx.application.LauncherImpl$$Lambda$51/1616271442.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
    at com.sun.javafx.application.PlatformImpl$$Lambda$44/1051754451.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/1814330183.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
    at com.sun.javafx.application.PlatformImpl$$Lambda$46/1775282465.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
    at com.sun.glass.ui.win.WinApplication$$Lambda$37/1109371569.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

This is line 44: File file = fileChooser.showOpenDialog(borderPane.getScene().getWindow()); 这是第44行: File file = fileChooser.showOpenDialog(borderPane.getScene().getWindow());

This is line 35: playView.setOnMouseClicked(event -> onPlayView()); 这是第35行: playView.setOnMouseClicked(event -> onPlayView());

Before this i used a StackPane and you had to click on a button to open the FileChooser and that worked fine but now im using a BorderPane and i want the FileChooser to open when the application opens. 在此之前,我使用了StackPane ,您必须单击一个按钮才能打开FileChooser ,并且工作正常,但现在我使用BorderPane并且我希望在应用程序打开时打开FileChooser And yes i set all the fx:id in the FXML file. 是的,我在FXML文件中设置了所有的fx:id

It is too soon to open the file dialog in the initialize method of a JavaFX controller. 以JavaFX控制器的initialize方法打开文件对话框还为时过早。 When this method is called, the FXML node tree is still in loading + initialization phase, so it has not been attached to the Scene yet. 调用此方法时,FXML节点树仍处于加载+初始化阶段,因此尚未将其附加到Scene

So getScene() returns null . 因此getScene()返回null

You have to call onStartup() later, when the scene graph has been fully initialized. 场景图已完全初始化后,您必须稍后调用onStartup() You can do that via Platform.runLater() : 您可以通过Platform.runLater()做到这一点:

@Override
public void initialize(URL location, ResourceBundle resources) {
    playView.setImage(new Image(getClass().getResource("Pause.png").toExternalForm()));
    playView.setVisible(false);
    playView.setOnMouseClicked(event -> onPlayView());
    Platform.runLater(this::onStartup);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM