JavaFX-主控制器调用导致空指针异常的子控制器功能

时间:2019-07-14 14:02:17

标签: java javafx model-view-controller controller fxml

我对JavaFX还是比较陌生,但我想弄清楚。

问题:当我的主控制器调用我的子控制器的功能时,只要它不需要操纵fxml文件的内容,它就可以完全正常运行。

令人惊讶的是,如果在initialize函数中操纵了fxml批注组件,则该代码不会引起任何错误。但是,每当我想在其他函数中使用它们时,都会抛出NullPointerException。

因此,在fxml注释组件下要更清楚一点,我的意思是这样的:@FXML private Label exampleLbl。

我创建了一个简化的示例:

主控制器类:

package application;

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

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Tab;
import javafx.scene.layout.AnchorPane;

public class MainController implements Initializable {

    @FXML SubController subController;

    @FXML private Tab Tab1;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        subController = new SubController();

        FXMLLoader loader = new FXMLLoader();

        try {
            AnchorPane anch1 = loader.load(getClass().getResource("SubView.fxml"));
            Tab1.setContent(anch1);
        }
        catch(Exception e) {
            System.out.println("unable to load tab1");
            e.printStackTrace();
        }
    }

    public void fooFunction() {
        if(subController != null) {
            subController.testFunction();   
        }
    }
}

子控制器类:

package application;

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

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

public class SubController implements Initializable {

    @FXML private Label exampleLbl;

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        if(exampleLbl != null) {
            System.out.println("It is printed, so it is not null");
            exampleLbl.setText("So I can manipulate this as I want.");
        }

    }

    public void testFunction() {
        if(exampleLbl == null) {
            System.out.println("It is printed, so it is null now.");
            exampleLbl.setText("But not here. It will throw an error.");
        }
    }
}

nullptrexception发生在testFunction()方法的第26行的SubController的类上。

行:exampleLbl.setText(“但不在这里。它将引发错误。”);

因此它在初始化函数中工作正常,但在testFunction中工作

这是我的FXML文件:

MainView fxml:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
   <center>
   <TabPane prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE">
        <tabs>
            <Tab fx:id="Tab1" text="Test">
               <content>

               </content>
            </Tab>
        </tabs>
   </TabPane>
   </center>

   <bottom>
        <Button mnemonicParsing="false" onAction="#fooFunction" text="Button" BorderPane.alignment="CENTER" />
   </bottom>
</BorderPane>

SubView FXML:

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

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

<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SubController">
      <Label fx:id="exampleLbl" layoutX="5.0" layoutY="128.0" prefHeight="17.0" prefWidth="188.0" text="Label" />
</AnchorPane>

以下是错误消息和控制台输出(如果有帮助的话):

It is printed, so it is not null
It is printed, so it is null now.

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8411)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$358(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
    at java.lang.Thread.run(Unknown Source)
Caused by: 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 sun.reflect.misc.Trampoline.invoke(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
    ... 48 more
Caused by: java.lang.NullPointerException
    at application.SubController.testFunction(SubController.java:26)
    at application.MainController.fooFunction(MainController.java:36)
    ... 58 more

因此,如果有人能解释我在这里缺少什么并提供简单的解决方案,我将不胜感激。

2 个答案:

答案 0 :(得分:0)

您在SubController中创建的initialize实例未与fxml文件一起使用。 FXMLLoader创建SubController 另一个实例 ,注入Label并为其调用initialize

您需要使用非static load方法来加载fxml文件并随后检索由FXMLLoader创建的控制器。 (load(URL)static。)没有必要对未注入@FXMLsubController)的字段进行注释。

MainController

@Override
public void initialize(URL location, ResourceBundle resources) {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("SubView.fxml"));

    try {
        AnchorPane anch1 = loader.load();
        Tab1.setContent(anch1);

        subController = loader.getController(); // retrieve controller
    } catch(Exception e) {
        System.out.println("unable to load tab1");
        e.printStackTrace();
    }
}

答案 1 :(得分:0)

使用subController = new SubController()时,刚创建的实例与FXML文件无关。因为控制器实例不是由FXMLLoader管理的,所以不会注入任何字段。就您而言,使用以下方法应该可以解决问题:

@Override
public void initialize(URL location, ResourceBundle resources) {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("SubView.fxml"));

    try {
        AnchorPane anch1 = loader.load();
        subController = loader.getController();
        Tab1.setContent(anch1);
    } catch (Exception e) {
        System.out.println("unable to load tab1");
        e.printStackTrace();
    }
}

请注意两件事:

  1. 我在构建期间设置了FXMLLoader的位置。使用loader.load(/* URL */)时,实际上是在使用 static FXMLLoader#load(URL)方法。您需要使用实例 load()方法。
  2. 我为subController分配了@FXML字段(顺便说一下,不必用loader.getController()注释)。该方法将返回根据您通过FXML文件中的FXMLLoader属性指定的类名创建的fx:controller实例。 重要提示:您必须在getController()之后致电load()

您可能还想考虑使用fx:include

MainView.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
   <center>
   <TabPane prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE">
        <tabs>
            <Tab fx:id="Tab1" text="Test">
               <content>
                   <!-- replace the value of source as needed -->
                   <fx:include source="SubView.fxml" fx:id="sub"/>
               </content>
            </Tab>
        </tabs>
   </TabPane>
   </center>

   <bottom>
        <Button mnemonicParsing="false" onAction="#fooFunction" text="Button" BorderPane.alignment="CENTER" />
   </bottom>
</BorderPane>

这使得不再需要在SubView.fxml方法内部手动加载MainController#initialize文件。另外,由于我为包含的文件分配了fx:id,因此SubController实例将为您注入。当涉及到注入控制器时,FXMLLoader会寻找名称为<fx:id>Controller(请参阅Nested Controllers)的适当键入(并带有注释)的字段。