我的解决方案
最后,问题取决于我在初始化方法中实例化了仅存在于 principal.fxml 上的ImageView的事实,因此,每当我开始一个新场景时,控制器都会寻找该ImageView,从而提供了NullPointerException。 我只是删除了该图像的初始化,并通过fxml添加了它。我不知道这是否是最好的方法,但是对我来说效果很好。
我有一个具有以下类和FXML文件的JavaFX应用程序:
我的应用程序以 principal.fxml 开头。在获取XML文件供以后处理之后,用户单击主体场景上的按钮后,我想打开我的 test.fxml 场景。 最后,在测试场景中,再次当用户单击按钮时,我想打开 final.fxml 场景。
我的项目结构如下:
├── res
│ ├── css
│ │ └── estils.css
│ ├── imatges
│ │ └── logoFinal.png
│ ├── view
│ │ ├── final.fxml
│ │ ├── principal.fxml
│ │ └── test.fxml
│ └── xml
│ └── testAngles.xml
├── SERRANO_PEJO_M03UF5.iml
└── src
└── jtest
├── Controlador.java
└── Main.java
我的 Main.java 代码如下:
package jtest;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/principal.fxml"));
Parent root = loader.load();
/*Controlador controlador = (Controlador)loader.getController();
controlador.setStageAnd*/
primaryStage.setTitle("jTest");
Scene escena = new Scene(root, 640, 400);
escena.getStylesheets().add(this.getClass().getResource("/css/estils.css").toExternalForm());
primaryStage.setScene(escena);
primaryStage.setResizable(false);
primaryStage.centerOnScreen();
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
打开我的新场景的 Action方法(在Controller.java中)如下:
@FXML
public void carregaTestAction (ActionEvent event) {
// Mostra un diàleg amb la selecció de l'arxiu
FileChooser seleccioArxiu = new FileChooser();
seleccioArxiu.setTitle("Selecciona l'arxiu de test");
arxiuTest = seleccioArxiu.showOpenDialog(btCarrega.getScene().getWindow());
// Carrega el nou stage, TEST
stage = (Stage) btCarrega.getScene().getWindow();
Parent root;
try {
root = FXMLLoader.load(getClass().getResource("/view/test.fxml"));
Scene sceneTest = new Scene(root, 640, 400);
stage.setScene(sceneTest);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
这是我的错误堆栈跟踪:
Stacktrace:
javafx.fxml.LoadException:
/home/biel/Documents/M03UF5/SERRANO_PEJO_M03UF5/out/production/SERRANO_PEJO_M03UF5/view/test.fxml
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at jtest.Controlador.carregaTestAction(Controlador.java:148)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1782)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8879)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:200)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3851)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.NullPointerException
at jtest.Controlador.initialize(Controlador.java:95)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
... 66 more
我也仅尝试使用此代码(这给了我们我们的老师),但它也给了我一个错误:
@FXML
public void carregaTestAction (ActionEvent event) {
// Mostra un diàleg amb la selecció de l'arxiu
FileChooser seleccioArxiu = new FileChooser();
seleccioArxiu.setTitle("Selecciona l'arxiu de test");
arxiuTest = seleccioArxiu.showOpenDialog(btCarrega.getScene().getWindow());
// Carrega el nou stage, TEST
stage = (Stage) btCarrega.getScene().getWindow();
Parent root;
try {
root = FXMLLoader.load(getClass().getResource("/view/test.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
}
我搜索并找到了Javafx: Open a new FXML from another Java Class,"FXMLLoader.constructLoadException" when trying to run java fx application和JavaFX open a new scene,但是我仍然不明白它应该如何工作以及为什么会给我这个错误。
编辑:我知道NullPointerException是什么,但是我不知道如何,即使有了代码,也无法打开新场景。我认为可能是因为它没有正确地获取我的 test.fxml 文件,但是它正在获取我的 principal.fxml 文件而在我的主类中没有任何问题,以及Controller类中的图片文件。
编辑2(2019-03-22 00:31):我修改了我的Stacktrace,现在我明白了为什么你向我提出了NullPointer问题。 在Controller类的initialize方法中,仅针对我的 principal.fxml 场景获得一个图像:
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
// PRINCIPAL.FXML
// Insereix la imatge del logo
Image imatgeLogo = new Image(this.getClass().getResourceAsStream("/imatges/logoFinal.png"));
ivLogo.setImage(imatgeLogo);
}
从此异常中我了解到的是,当我尝试初始化其他阶段时,控制器找不到要获取的ImageView吗?如果是这样,我如何才能使控制器仅在开始 principal.fxml 场景时获取图像?不便之处,敬请原谅。