使用JavaFX ToggleButton更改布尔值时发生InvocationTarget错误

时间:2018-11-01 18:28:22

标签: java intellij-idea javafx

我一直试图将JavaFX集成到我的应用程序中,但我一直遇到此错误

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: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:394)
    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$2(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    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.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$5(GtkApplication.java:139)
    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.GeneratedMethodAccessor10.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)
    ... 29 more
Caused by: java.lang.NullPointerException
    at Structured.SquareToggle(Structured.java:850)
    ... 39 more

这是我的主类中的一部分,在该部分中,我试图通过JavaFX Toggle将布尔值设置为true / false:

    @FXML
    private void LineToggle(ActionEvent actionEvent) {
        toggleLine = ToggleLine.isSelected();
    }

    @FXML
    private void SquareToggle(ActionEvent actionEvent) {
        toggleSquare = ToggleSquare.isSelected();
    }

    public void CircleToggle(ActionEvent actionEvent) {
        toggleCircle = ToggleCircle.isSelected();
    }

    public void TriangleToggle(ActionEvent actionEvent) {
        toggleTriangle = ToggleTriangle.isSelected();
    }

    public void HexagonToggle(ActionEvent actionEvent) {
        toggleHex = ToggleHexagon.isSelected();
    }

    public void CubeToggle(ActionEvent actionEvent) {
        toggleCube = ToggleCube.isSelected();
    }

    public void SquareFillToggle(ActionEvent actionEvent) {
        toggleSquareFill = ToggleSquareFill.isSelected();
    }

    public void CircleFillToggle(ActionEvent actionEvent) {
        toggleCircleFill = ToggleCircleFill.isSelected();
    }

    public void TriangleFillToggle(ActionEvent actionEvent) {
        toggleTriangleFill = ToggleTriangleFill.isSelected();
    }

    public void HexagonFillToggle(ActionEvent actionEvent) {
        toggleHexFill = ToggleHexagonFill.isSelected();
    }

    public void CubeFillToggle(ActionEvent actionEvent) {
        toggleCubeFill = ToggleCubeFill.isSelected();
    }

这是我的JavaFX fxml文件,供参考:

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

<?import com.jfoenix.controls.JFXSlider?>
<?import com.jfoenix.controls.JFXTextField?>
<?import com.jfoenix.controls.JFXToggleNode?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="365.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Structured">
   <JFXToggleNode fx:id="ToggleLine" layoutX="10.0" layoutY="5.0" mnemonicParsing="false" onAction="#LineToggle" style="-fx-pref-width: 150; -fx-background-color: #d1d3d1;" text="Toggle Line" AnchorPane.bottomAnchor="349.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="340.0" AnchorPane.topAnchor="5.0" />
   <JFXToggleNode fx:id="ToggleSquare" layoutX="10.0" layoutY="35.0" mnemonicParsing="false" onMousePressed="#SquareToggle" style="-fx-pref-width: 150; -fx-background-color: #d1d3d1;" text="Toggle Square" />
   <JFXToggleNode fx:id="ToggleCircle" layoutX="10.0" layoutY="65.0" mnemonicParsing="false" onAction="#CircleToggle" style="-fx-pref-width: 150; -fx-background-color: #d1d3d1;" text="Toggle Circle" />
   <JFXToggleNode fx:id="ToggleTriangle" layoutX="10.0" layoutY="95.0" mnemonicParsing="false" onAction="#TriangleToggle" style="-fx-pref-width: 150; -fx-background-color: #d1d3d1;" text="Toggle Triangle" />
   <JFXToggleNode fx:id="ToggleHexagon" layoutX="10.0" layoutY="125.0" mnemonicParsing="false" onAction="#HexagonToggle" style="-fx-pref-width: 150; -fx-background-color: #d1d3d1;" text="Toggle Hexagon" />
   <JFXToggleNode fx:id="ToggleCube" layoutX="10.0" layoutY="155.0" mnemonicParsing="false" onAction="#CubeToggle" style="-fx-pref-width: 150; -fx-background-color: #d1d3d1;" text="Toggle Cube" />
   <JFXToggleNode fx:id="ToggleSquareFill" layoutX="10.0" layoutY="215.0" mnemonicParsing="false" onAction="#SquareFillToggle" style="-fx-pref-width: 150; -fx-background-color: #d1d3d1;" text="Toggle Square Fill" />
   <JFXToggleNode fx:id="ToggleCircleFill" layoutX="10.0" layoutY="245.0" mnemonicParsing="false" onAction="#CircleFillToggle" style="-fx-pref-width: 150; -fx-background-color: #d1d3d1;" text="Toggle Circle FIll" />
   <JFXToggleNode fx:id="ToggleTriangleFill" layoutX="10.0" layoutY="275.0" mnemonicParsing="false" onAction="#TriangleFillToggle" style="-fx-pref-width: 150; -fx-background-color: #d1d3d1;" text="Toggle Triangle Fill" />
   <JFXToggleNode fx:id="ToggleHexagonFill" layoutX="10.0" layoutY="305.0" mnemonicParsing="false" onAction="#HexagonFillToggle" style="-fx-pref-width: 150; -fx-background-color: #d1d3d1;" text="Toggle Hexagon Fill" />
   <JFXToggleNode fx:id="ToggleCubeFill" layoutX="10.0" layoutY="335.0" mnemonicParsing="false" onAction="#CubeFillToggle" style="-fx-pref-width: 150; -fx-background-color: #d1d3d1;" text="Toggle Cube Fill" />
   <JFXSlider fx:id="AlphaSlider" indicatorPosition="RIGHT" layoutX="180.0" layoutY="10.0" onDragDetected="#AlphaSliderSync" AnchorPane.bottomAnchor="354.0" />
   <JFXTextField fx:id="AlphaBox" layoutX="333.0" layoutY="5.0" prefHeight="26.0" prefWidth="100.0" />
</AnchorPane>

这是我的控制器类:

import com.jfoenix.controls.JFXToggleNode;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class Controls extends Application {

    @FXML
    private JFXToggleNode ToggleLine;
    @FXML
    private JFXToggleNode ToggleSquare;
    @FXML
    private JFXToggleNode ToggleCircle;
    @FXML
    private JFXToggleNode ToggleTriangle;
    @FXML
    private JFXToggleNode ToggleHexagon;
    @FXML
    private JFXToggleNode ToggleCube;
    private Structured mainApp;

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

    @Override
    public void start(Stage primaryStage) throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("Controls.fxml"));
        Parent root = loader.load();
        Scene scene = new Scene(root);
        scene.getStylesheets().add("root.css");
        primaryStage.setScene(scene);
        primaryStage.setAlwaysOnTop(true);
        primaryStage.show();
        primaryStage.setOnCloseRequest(t -> {
            Platform.exit();
            System.exit(0);
        });
    }



    public void setMainApp(Structured mainApp) {
        // this.mainApp = mainApp;

        // pass the main app to the drawerContentController:
        this.mainApp = mainApp;
    }

}

单击我的ToggleButton试图在MainClass中设置布尔值时,会发生此错误。我试图浏览其他StackOverflow帖子,但未能实现他们显示的任何建议

JavaFX: Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

JavaFX FXMLLoader InvocationTargetException?

JavaFX InvocationTargetException FXML

0 个答案:

没有答案