gpt4 book ai didi

java - JavaFX 中的 FileChooser 给出 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 03:36:48 25 4
gpt4 key购买 nike

这是我的课:

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);
}
}
}

我收到此错误:

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)

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

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

在此之前,我使用了 StackPane,您必须单击一个按钮才能打开 FileChooser,效果很好,但现在我使用 BorderPane code> 并且我希望在应用程序打开时打开 FileChooser 。是的,我在 FXML 文件中设置了所有 fx:id

最佳答案

在 JavaFX Controller 的 initialize 方法中打开文件对话框还为时过早。调用此方法时,FXML节点树仍处于加载+初始化阶段,因此尚未附加到Scene

因此 getScene() 返回 null

当场景图完全初始化后,您必须稍后调用onStartup()。您可以通过 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);
}

关于java - JavaFX 中的 FileChooser 给出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27705244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com