用于扩展javafx.scene.shape.Circle的类的InvocationTargetException

时间:2019-02-22 04:59:45

标签: java javafx javafx-8

我正在通过制作一个旨在测试鼠标准确性的游戏来进行我的第一个JavaFX辅助项目(如在AimBooster.com上所见)。这个游戏的主要元素是目标,我在Target.java类中描述了它的行为,该类扩展了Circle类。这是我的课程定义(在“公共课程目标扩展圈子”内):

Circle target;
double centerX;
double centerY;
double radius;

StackPane game;

public Target(double x, double y, double rad) {

    centerX = x;
    centerY = y;
    radius = rad;

    target.setCenterX(centerX); // InvocationTargetException here
    target.setCenterY(centerY);
    target.setRadius(radius);


}

这一切似乎超级简单,但是当我打电话时

target = new Target(200, 200, 10);

我收到一个InvocationTargetException。

我已经对此异常进行了一些研究,但没有发现适用于JavaFX的东西。我在做什么错了?

编辑:完整堆栈跟踪:

Exception in Application start method
Exception in thread "main" 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.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
    at Target.<init>(Target.java:19)
    at GraphicsDemo.start(GraphicsDemo.java:168)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    ... 1 more

1 个答案:

答案 0 :(得分:0)

首先,这应该对未初始化的圈子有所帮助。

Circle target = new Circle(); //create an instance.
double centerX;
double centerY;
double radius;

StackPane game;

public Target(double x, double y, double rad) {

    centerX = x;
    centerY = y;
    radius = rad;

    target.setCenterX(centerX); // InvocationTargetException here
    target.setCenterY(centerY);
    target.setRadius(radius);


}