JavaFX Problem z przyciskami

0

Mam problem z JavąFX, tak jak pisaliście zrobiłem projekt Mavenowy, ale teraz mam inny problem. Gdy klikam którykolwiek przycisk wyświetla się ten błąd:

Error:
Button Clicked: RESET
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1857)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1724)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8792)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3897)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1878)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2623)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:557)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:943)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:290)
at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:77)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1852)
... 46 more
Caused by: java.lang.NullPointerException
at me.panevenciarz.Maven.Controller.reset(Controller.java:16)
... 57 more

Link do projektu: https://drive.google.com/file/d/1b1QK50uIsMopNDqpG_J7FVqwbcdqM3JS/view?usp=sharing

Plik FXML:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.Circle?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="420.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="me.panevenciarz.Maven.Controller">
   <children>
      <Circle fill="DODGERBLUE" layoutX="320.0" layoutY="210.0" radius="50.0" stroke="BLACK" strokeType="INSIDE" />
      <Button layoutX="537.0" layoutY="26.0" mnemonicParsing="false" onAction="#up" text="Up" />
      <Button layoutX="528.0" layoutY="78.0" mnemonicParsing="false" onAction="#down" text="Down" />
      <Button layoutX="529.0" layoutY="52.0" mnemonicParsing="false" onAction="#reset" text="Reset" />
      <Button layoutX="487.0" layoutY="52.0" mnemonicParsing="false" onAction="#left" text="Left" />
      <Button layoutX="581.0" layoutY="52.0" mnemonicParsing="false" onAction="#right" text="Right" />
   </children>
</AnchorPane>

Plik kontrolera

package me.panevenciarz.Maven;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.shape.Circle;

public class Controller {

    @FXML
    private Circle myCircle;
    public static double x;
    public static double y;

    public void reset(ActionEvent e) {
        System.out.println("Button Clicked: RESET");
        myCircle.setCenterY(0);
        myCircle.setCenterX(0);
    }
    public void up(ActionEvent e) {
        System.out.println("Button Clicked: UP");
        myCircle.setCenterY(y-=5);
    }
    public void down(ActionEvent e) {
        System.out.println("Button Clicked: DOWN");
        myCircle.setCenterY(y+=5);
    }
    public void right(ActionEvent e) {
        System.out.println("Button Clicked: RIGHT");
        myCircle.setCenterX(x-=5);
    }
    public void left(ActionEvent e) {
        System.out.println("Button Clicked: LEFT");
        if (x == 640) {
            myCircle.setCenterX(x+=5);
        }


    }


}

Plik Main

package me.panevenciarz.Maven;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;

import java.io.File;

public class Main extends Application {

    public Label welcomeText;

    @Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader loader = new FXMLLoader(new File("/home/mikolaj/Dokumenty/Java/AcktualneProjekty/MavenFX2/src/main/resources/com/example/demo/hello-view.fxml").toURI().toURL());
        Parent root = loader.load();
        primaryStage.setResizable(false);
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 640, 420));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}
3

at me.panevenciarz.Maven.Controller.reset(Controller.java:16)

Czyli tutaj

        myCircle.setCenterY(0);

obiekt myCircle jest nullem. Nie jestem specjalistą od JavyFX ale skąd niby ten kod ma wiedzieć co gdzie wstrzyknąć? Czy nazwa pola w klasie kontrolera nie musi być spólna z jakimś astrybutem id w tym xmlu? Przecież mógłbyś tam mieć dwa Circle i jak by miało zadziałać? Jesteś pewien że ten Circle w xmlu nie powinien mieć jakiegoś fx:id="myCircle"?

0

Dodatkowo dodam, że nie powinieneś ładować widoku w taki sposób. Lepiej to robić tak: FXMLLoader.load(getClass().getResource("XXX"));
Gdzie XXX, to ścieżka do widoku. Umieszczasz je np. w resources/fxml/hello-view.fxml

1 użytkowników online, w tym zalogowanych: 0, gości: 1