0

I am doing my first JavaFX side project by making a game that is meant to test mouse accuracy (as seen on AimBooster.com). The main elements of this game are targets, the behavior of which I am describing in the class Target.java, which extends class Circle. Here is my class definition (within the block "public class Target extends 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);


}

This all seems super straightforward, but when I call

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

I get an InvocationTargetException.

I have done some research regarding this exception, but I have found nothing applicable to JavaFX. What am I doing wrong?

Edit: full stack trace:

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
Jacob H
  • 1
  • 1
  • Please post the whole stack trace. The `InvocationTargetException` is likely not the root cause. Look through the full error at the "Caused By:" sections. – Zephyr Feb 22 '19 at 05:00
  • Also, why are you declaring a field `Target target` within the `Target` class? Is that intended? It looks like you are trying to use `target` within your constructor, which would be `null` at this point... A [mcve] would be very helpful here. – Zephyr Feb 22 '19 at 05:01
  • There you go. The issue is `NullPointerException`. – Zephyr Feb 22 '19 at 05:02
  • Got it, it's been a while since I had to write my own classes so I forgot how to write a child constructor. Yikes! Most of the assignments I've had lately have the classes already defined. – Jacob H Feb 22 '19 at 05:07
  • "Circle target;" it is never initialized. So "Circle target = new Circle();" – kai Feb 22 '19 at 07:32
  • I should mention I fixed it last night: I needed to call the super() reference from class Circle. – Jacob H Feb 22 '19 at 21:35

1 Answers1

0

For a start, this should help for the uninitalized Circle.

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);


}
kai
  • 894
  • 1
  • 7
  • 15