gpt4 book ai didi

按下 Pane 中的按钮时发生 JavaFX InvokingTargetException

转载 作者:行者123 更新时间:2023-12-02 11:25:21 26 4
gpt4 key购买 nike

我只是想从我的 JavaFX 项目中的另一个类中划出一条线(作为一些家庭作业的开始)。但我遇到了这个 InitationTargetException 以及 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

我已经尝试过this answer来自 James_D,但在尝试他的解决方案后仍然无法运行。

主要:

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>

Controller :

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

kruAlg.drawMaze();

// kruAlg.solveMaze();

}

克鲁什卡尔A级:

    @FXML
Canvas canvas;
GraphicsContext gc;


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

也许这就是我想做的;从 Controller 运行 KrushkalA 类中的方法,该方法会在按下按钮时在 Canvas 内绘制一条线或执行某些操作。

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

最佳答案

您的代码不起作用的原因是您期望 canvas从 FXML 注入(inject)到您的 KrushkalA实例。这根本不是它的工作原理:@FXML -注入(inject)由 FXMLLoader 完成当加载 FXML 文件时,以及任何 @FXML - Controller 实例中的带注释字段使用 FXML 文件中匹配 fx:id 的相应元素进行初始化。即使您添加 fx:id属性到 FXML 中定义的 Canvas , FXMLLoader 不可能可能知道KrushkalA对象(当时甚至还没有创建,尽管如果创建了也无济于事)。

无论如何,这里的整个前提似乎都有缺陷。允许对 UI 元素的引用从 Controller 中逃逸通常是一个坏主意。假设在未来的某个时刻,您决定不想用 Canvas 来绘制它。更多(例如,您可能决定将 Line 或其他节点添加到 Pane )。在这种情况下,您实际上只需要修改 FXML 和 Controller 类即可;如果您允许您的KrushkalA类访问 Canvas ,那么您也需要更改它,并且您需要查看该类是否将 Canvas 引用传递给其他任何人。如果您以这种方式破坏封装,那么维护它就会变得更加困难。

如果您的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 ;
}
}

然后回到你的 Controller ,你会这样做:

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"<Canvas> FXML 中的元素。

这种方法很好地管理了关注点分离,并遵守单一职责原则: Controller 只负责更新 UI,并不关心算法如何实现,KrushkalA类只负责实现算法,并不关心 UI 是如何渲染的(或者即使有 UI...)。

<小时/>

如果您确实希望算法类在 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...
}
}

关于按下 Pane 中的按钮时发生 JavaFX InvokingTargetException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49678373/

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