在窗格中按下按钮时出现 JavaFX InvocationTargetException

JavaFX InvocationTargetException when pressing button in Pane

我只是想从我的 JavaFX 项目中的另一个 class 画一条线(作为一些家庭作业的开始)。但是我 运行 正在进入此 InvocationTargetException 以及 NullPointerException 异常。

"C:\Program Files\Java\jdk1.8.0_144\bin\java" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2017.2.4\lib\idea_rt.jar=58260:C:\Program Files\JetBrains\IntelliJ IDEA 2017.2.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_144\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\rt.jar;C:\Users\caspe\Desktop\myFinalMiniProject\out\production\myFinalMiniProject" sample.Main
drawing maze..
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:8413)
    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.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase.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.access00(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:381)
    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$handleMouseEvent4(GlassViewEventHandler.java:417)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
    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$null8(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    ... 48 more
Caused by: java.lang.NullPointerException
    at sample.KrushkalA.drawMaze(KrushkalA.java:23)
    at sample.Controller.doKruskal(Controller.java:13)
    ... 58 more

我已经尝试了 James_D 中的 this answer,但在尝试了他的解决方案后仍然不会 运行。

主要:

public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 350, 350));
        primaryStage.show();
    }

FXML:

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

<?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="350.0"
      prefWidth="350.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
    <children>
        <Button fx:id="buttonKruskal" layoutX="14.0" layoutY="2.0" mnemonicParsing="false" onAction="#doKruskal"
                prefHeight="20.0" prefWidth="100.0" text="Kruskal"/>
        <Button fx:id="buttonRecursiveD" layoutX="236.0" layoutY="2.0" mnemonicParsing="false" onAction="#doRecursiveD"
                prefHeight="20.0" prefWidth="100.0" text="RecursiveD"/>
        <Button fx:id="buttonRecursiveB" layoutX="125.0" layoutY="2.0" mnemonicParsing="false" onAction="#doRecursiveB"
                prefHeight="20.0" prefWidth="100.0" text="RecursiveB"/>
        <Canvas height="300.0" layoutX="25.0" layoutY="36.0" width="300.0"/>
    </children>
</Pane>

控制器:

public void doKruskal(ActionEvent event) {
    System.out.println("drawing maze..");
    KrushkalA kruAlg = new KrushkalA();

    kruAlg.drawMaze();

    // kruAlg.solveMaze();

}

KrushkalA class:

    @FXML
    Canvas canvas;
    GraphicsContext gc;


    public void drawMaze(){
        gc = canvas.getGraphicsContext2D();
        gc.strokeLine(0,0,300,50);
    }

也许这就是我想要做的; 运行 来自控制器的 KrushkalA class 中的一个方法,它在按下按钮时在 canvas 中绘制一条线或只是做一些事情。

这是错误的做法吗?我可以 运行 在我的控制器 class 中导致异常的代码,但不能来自任何其他 class?示例代码将不胜感激。

您的代码不起作用的原因是您希望 canvas 从 FXML 注入到您的 KrushkalA 实例中。这根本不是它的工作原理:@FXML-注入是在加载 FXML 文件时由 FXMLLoader 完成的,并且控制器中任何 @FXML-注释字段 instance 使用 FXML 文件中匹配 fx:id 的相应元素进行初始化。即使您将 fx:id 属性添加到 FXML 中定义的 canvas,FXMLLoader 也无法知道 KrushkalA 对象(甚至不是那个时候创建​​的,虽然如果是的话也无济于事)。

无论如何,这里的整个前提似乎都有缺陷。允许对 UI 元素的引用从控制器中转义通常不是一个好主意。假设在未来的某个时候,您决定不再使用 Canvas 来绘制它(例如,您可能决定将 Line 或其他节点添加到 Pane).在这种情况下,您真的只需要修改 FXML 和控制器 class;如果您允许 KrushkalA class 访问 canvas,那么您也需要更改它,并且您需要查看 class 是否通过了canvas 提及其他任何人。如果以这种方式破坏封装,维护它会变得更加困难。

如果你的 KrushkalA class 真的只是在实现算法,它应该与 UI 没有任何关系。我认为您正在创建一个迷宫(或者,更抽象地说,一个图形的生成树),因此您的 drawMaze() 方法应该只计算迷宫和 return 结果。在它的核心,迷宫只是单元格(节点)之间的边界(或边)的集合,所以我想我会定义一个 Boundary class 并用一些简单的表示它是哪个边界(也许int xint yboolean horizontal 字段,或其他方便的字段);那么你将有:

public class KrushkalA {

    public List<Boundary> drawMaze() {
        List<Boundary> maze = new ArrayList<>();
        // implementation of algorithm...
        return maze ;
    }
}

然后回到你的控制器,你会做:

public class MazeController {

    @FXML
    private Canvas canvas ;

    @FXML
    private void doKrushkal(ActionEvent e) {
        KrushkalA kruAlg = new KrushkalA();
        List<Boundary> maze = kruAlg.drawMaze();
        // now render maze on canvas, using data in the List<Boundary> from above
        renderMaze(maze);
    }

    private void renderMaze(List<Boundary> maze) {
        // clear canvas, iterate through boundaries in maze,
        // and draw lines in appropriate place, etc.
    }
}

当然,还要将 fx:id="canvas" 添加到 FXML 中的 <Canvas> 元素。

这种方法妥善地管理了关注点分离并遵守单一职责原则:控制器只负责更新 UI,并不关心算法是如何实现的,KrushkalA class 只负责实现算法,并不关心 UI 是如何呈现的(或者即使有 一个 UI...).


如果你真的想要算法 class 在 canvas 上渲染迷宫,我 强烈不推荐, 那么你当然可以只需将 canvas 传递给它:

    @FXML
    private void doKrushkal(ActionEvent e) {
        KrushkalA kruAlg = new KrushkalA();
        kruAlg.drawMaze(canvas);
    }

public class KrushkalA {

    public void drawMaze(Canvas canvas) {
        // compute maze AND render it on canvas...
    }
}