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

时间:2018-04-05 17:40:34

标签: java javafx nullpointerexception invocationtargetexception

我只是想在我的JavaFX项目中从另一个类中绘制一条线(作为一些功课的开始)。但是我遇到了这个 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$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: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$handleMouseEvent$354(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$null$148(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班:

    @FXML
    Canvas canvas;
    GraphicsContext gc;


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

也许这就是我想要做的;从控制器运行KrushkalA类中的方法,当按下按钮时,该方法在画布中绘制一条线或只是做某事。

这是错误的做法吗?我可以运行导致我的Controller类中的异常的代码,但不能从任何其他类运行?示例代码将非常感激。

1 个答案:

答案 0 :(得分:1)

您的代码无法正常工作的原因是您期望canvas从FXML注入您的KrushkalA实例。这根本不是这样的:@FXML - 注入是在加载FXML文件时由FXMLLoader完成的,以及控制器中的任何@FXML - 注释字段实例使用匹配的fx:id在FXML文件中使用相应的元素进行初始化。即使您在FXML中定义的画布上添加fx:id属性,FXMLLoader也无法了解KrushkalA对象(不是Canvas对象。甚至在那个时候创造,但如果是的话它也不会有帮助。

无论如何,这里的整个前提似乎都有缺陷。允许对UI元素的引用从控制器中逃脱通常是个坏主意。假设在将来某个时候您决定不再使用Line来绘制它(例如,您可能决定将Pane或其他节点添加到{ {1}})。在这种情况下,你真的应该只需要修改FXML和控制器类;如果您允许KrushkalA类访问画布,那么您也需要更改它,并且您需要查看该类是否将画布引用传递给其他任何人。如果以这种方式打破封装,维持这一点会变得更加困难。

如果您的KrushkalA类实际上只是实现了算法,那么它就不应该与UI有任何关系。我想你在这里创建一个迷宫(或者更抽象地说,是一个图形的生成树),所以你的drawMaze()方法应该只计算迷宫并返回结果。在它的核心,迷宫只是单元格(节点)之间的边界(或边缘)的集合,所以我想我会定义一个Boundary类,其中有一个简单的表示它是哪个边界(也许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 ......)。

如果你真的希望算法类在画布上渲染迷宫,我强烈建议,那么你当然可以将画布传递给它:

    @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...
    }
}