gpt4 book ai didi

java - NullPointerException 从 FXML 加载场景图,与 Spring 集成

转载 作者:太空宇宙 更新时间:2023-11-04 09:53:04 35 4
gpt4 key购买 nike

我想为我的 JavaFX 应用程序构造一个 Controller ,它自动将 FXML 文件“view.fxml”加载到成员“Parent root”中,并采用构造函数参数(在本例中为“String message”)。

我使它工作得很好,但后来我尝试使用 Spring 实例化 DemoController 实例,并且收到“NullPointerException:Root 不能为空”。这让我很恼火,因为使用 Spring 的 bean 实例化似乎工作得很好,但它没有正确加载 FXML。我唯一的猜测是目录结构可能会困惑,但我无法修复它,如果有任何帮助,我将非常感激:)

Main.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
DemoController myController = (DemoController) context.getBean("myController");

primaryStage.setScene(new Scene(myController.getRoot()));
primaryStage.setTitle("Game of Life");
primaryStage.show();

((ClassPathXmlApplicationContext) context).close();
}
}

FXMLController.java

import java.io.IOException;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;


public abstract class FXMLController implements Initializable {

protected Parent root;

protected String fxmlFilePath;

public void afterPropertiesSet() throws Exception {
loadFXML();
}

protected final void loadFXML() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource(fxmlFilePath));
loader.setController(this);
this.root = loader.load();
}

public Parent getRoot() {
return root;
}

public void setFxmlFilePath(String fxmlFilePath) {
this.fxmlFilePath = fxmlFilePath;
}
}

DemoController.java

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

public class DemoController extends FXMLController {

public DemoController(String message) {
System.out.println(message);
}

@Override
public void initialize(URL arg0, ResourceBundle arg1) {
System.out.println("initializing");
}

}

Beans.xml

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

<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id = "myController" class = "DemoController">
<constructor-arg value = "message"/>
<property name = "fxmlFilePath" value = "/view.fxml"/>
</bean>

</beans>

View .fxml

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

<?import javafx.scene.layout.AnchorPane?>

<AnchorPane xmlns:fx="http://javafx.com/fxml/1">
<!-- TODO Add Nodes -->
</AnchorPane>

目录结构

  • 源代码
    • Beans.xml
    • View .fxml
    • (默认包)
      • DemoController.java
      • FXMLController.java
      • Main.java

控制台输出

message
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Root cannot be null
at javafx.scene.Scene.<init>(Scene.java:336)
at javafx.scene.Scene.<init>(Scene.java:194)
at Main.start(Main.java:21)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
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$147(WinApplication.java:177)
... 1 more
Exception running application Main

代码简要概述:

  • 主要设置JavaFX环境并使用Spring实例化DemoController。它显示 Controller 给出的场景图。
  • FXMLController 是一个抽象类,它封装了 FXML 文件的加载并为场景图提供了 getter 方法 (getRoot())。
  • DemoController 扩展了 FXMLController,但没有添加太多内容。
  • Beans.xml 为 Spring 提供 Controller 所需的配置信息。
  • view.fxml 只有一个 AnchorPane,基本上是空的。

最佳答案

您没有实现InitializingBean ,因此 afterPropertiesSet 永远不会被调用。实现此接口(interface)应该可以解决问题:

public abstract class FXMLController implements Initializable, InitializingBean {

...

@Override
public void afterPropertiesSet() throws Exception {
loadFXML();
}

...

}

关于java - NullPointerException 从 FXML 加载场景图,与 Spring 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54461979/

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