Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Springbean给出了NullPointerException,但不是null_Java_Spring_Dependency Injection_Nullpointerexception - Fatal编程技术网

Java Springbean给出了NullPointerException,但不是null

Java Springbean给出了NullPointerException,但不是null,java,spring,dependency-injection,nullpointerexception,Java,Spring,Dependency Injection,Nullpointerexception,我正在使用Spring和XML配置来设置bean依赖项,在一个文本编辑器程序中,我正在使用JavaFXforGUI。对于文件选择器,我有一个文件管理器类,应该将应用程序的stage注入其中。我有两种方法打印Stage对象以查看它是否为null。在init()方法中,对象成功打印,输出javafx.stage。Stage@1cdcac0e。但是当我调用open()方法时,我得到了一个错误 首先,这是如何在beans.xml中配置我的bean: <?xml version = "1.0" en

我正在使用Spring和XML配置来设置bean依赖项,在一个文本编辑器程序中,我正在使用JavaFXforGUI。对于文件选择器,我有一个文件管理器类,应该将应用程序的stage注入其中。我有两种方法打印Stage对象以查看它是否为null。在
init()
方法中,对象成功打印,输出
javafx.stage。Stage@1cdcac0e
。但是当我调用
open()
方法时,我得到了一个错误

首先,这是如何在beans.xml中配置我的bean:

<?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="main" class="config.Main" scope="singleton" init-method="initiate">

    </bean>

    <bean id = "fileManager" class="files.FileManager" scope="singleton" init-method="init">
        <property name="ownerWindow" value="#{main.primaryStage}"/>
    </bean>

    <bean id = "editor" class = "controllers.Editor" init-method="init">
        <property name="fileManager">
            <bean id="fileManager" class="files.FileManager"/>
        </property>
    </bean>

</beans>
我的主要课程是:

package config;

public class Main extends Application {
    public static ApplicationContext context = new ClassPathXmlApplicationContext("/Beans.xml");
    FXMLLoader loader = new FXMLLoader();
    public Stage primaryStage = new Stage();
    Parent root;
    String rootPath = "/views/Editor.fxml";
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        root = loader.load(getClass().getResourceAsStream(rootPath));
        this.primaryStage.setScene(new Scene(root));
        this.primaryStage.show();

    }

    public void setPrimaryStage(Stage primaryStage) {
        this.primaryStage = primaryStage;
    }
    public void initiate() {
        System.out.println(primaryStage);
    }
}
Main
中的
initiate()
方法与FileManager中的
init()
方法打印的行相同,因此我假设Spring正在成功地将
primaryStage
注入FileManager类。 尽管如此,当我从第三个类调用
open()
时,即
Editor
类,我得到NullPointerException:

java.lang.NullPointerException
在controllers.Editor.openNewFile(Editor.java:15)

Editor
类是我的应用程序GUI的控制器,它有一个应该由Spring注入的FileManager实例,在它的
init()
方法中,它只打印FileManager对象,程序输出
文件。FileManager@668188db
,因此,我假设实例正在成功注入,并且不应为null。 我的编辑器类如下:

package controllers;

public class Editor {
    //Spring bean
    private FileManager fileManager;
    @FXML TextArea page;
    @FXML Menu menuFile;

    @FXML private void openNewFile() {
        fileManager.open();
    }

    public void setFileManager(FileManager fileManager) {
        this.fileManager = fileManager;
    }
    public void init() {
        System.out.println(fileManager);
    }

}
openNewFile()
方法是引发错误的地方。 我的fxml文件,以防有任何帮助:

<AnchorPane prefHeight="600.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1"
        fx:controller="controllers.Editor">
   <children>
       <MenuBar layoutX="188.0" layoutY="14.0" AnchorPane.bottomAnchor="575.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
           <Menu fx:id="menuFile" mnemonicParsing="false" text="File">
               <MenuItem text="New" onAction="#openNewFile"/>
               <MenuItem text="Open"/>
               <MenuItem text="Open Recent"/>
               <MenuItem text="Save"/>
               <MenuItem text="Save as"/>
               <MenuItem text="Close"/>
           </Menu>
       </MenuBar>
      <BorderPane layoutX="250.0" layoutY="178.0" AnchorPane.bottomAnchor="50.0" AnchorPane.leftAnchor="100.0" AnchorPane.rightAnchor="100.0" AnchorPane.topAnchor="100.0">
        <center>
            <AnchorPane prefHeight="550.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1">
                <children>
                    <TextArea fx:id="page" layoutX="186.0" layoutY="100.0" wrapText="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                        <padding>
                            <Insets right="10.0" />
                        </padding>
                    </TextArea>
                </children>
            </AnchorPane>
        </center>
      </BorderPane>
   </children>
</AnchorPane>

正如@Simon和@M.Deinum所指出的,抛出
NullPointerException
是因为Spring创建的
Editor
实例不是调用
openNewFile()
方法的instance,而是JavaFX控制我的实例。使用
loader.setController(context.getBean(“编辑器”)
而不是在fxml文件中设置JavaFX控制器解决了我的问题。
Main
类的
start()
方法现在如下所示:

public void start(Stage primaryStage) throws Exception {
    loader.setController(context.getBean("editor"));
    root = loader.load(getClass().getResourceAsStream(rootPath));
    this.primaryStage.setScene(new Scene(root));
    this.primaryStage.show();

}

您有两个
文件管理器
实例。一个有依赖项,另一个没有。您应该引用第一个
文件管理器
,而不是创建一个新的。您的NPE表明导致NPE的
编辑器
实例不是Spring创建的。您能告诉我们如何获得
编辑器
安装吗nce?(尽管如此,您还创建了2个
FileManager
实例;在
编辑器上使用
定义)@M.Deinum I删除了第二个
FileManager
(编辑器bean中的一个)但是我仍然得到相同的结果删除它不会有帮助,因为这会使它为空,您必须将对顶层的引用放在那里。同时在
bean中定义一些东西。xml
并不意味着这就是Java FX使用的实例,我怀疑您需要做更多的工作来进行适当的集成。JavaFX正在控制您的实例,而不是Sp所以用Spring来定义它,而不使用Spring控件,它不会真正起作用。
public void start(Stage primaryStage) throws Exception {
    loader.setController(context.getBean("editor"));
    root = loader.load(getClass().getResourceAsStream(rootPath));
    this.primaryStage.setScene(new Scene(root));
    this.primaryStage.show();

}