无法访问控制器内部的FXML窗格(NullPointerException)

时间:2019-05-02 07:45:50

标签: java javafx fxml

我想通过单击按钮将FXML加载到我的BorderPane中。

问题是,我单击按钮[语言]时会收到以下错误:

由以下原因引起:java.lang.NullPointerException     在control.options.options.onLanguageClick(options.java:83))

问题是我(可能发生错误?)无法访问控制器中的内容窗格。

view.options.Options.java

package view.options;

import constants.FileConstants;
import javafx.application.*;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class Options extends Application {

    @Override
    public void init() throws Exception {
        super.init();
        Font.loadFont(getClass().getResourceAsStream(FileConstants.OPTIONS_PATH + "/resources/options/Seattle_Avenue.ttf"), 14);
    }

    public static void main(String[] args) {
        Application.launch(args);
        System.out.println("exited");
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane mainBorderPane = FXMLLoader.load(Options.class.getResource("main.fxml"));
        primaryStage.setScene(new Scene(mainBorderPane));
        primaryStage.show();

    }

}

view.options.main.fxml

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

<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.Pane?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1280.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="control.options.options">
    <top>
        <fx:include source="title.fxml"/>
    </top>
    <bottom>
        <fx:include source="bottom.fxml"/>
    </bottom>
    <left>
        <fx:include source="left.fxml"/>
    </left>
    <center>
        <!--
        <fx:include source="resolutionSettings.fxml"/>
        -->
        <Pane fx:id="content"/>
    </center>
</BorderPane>

view.options.languageSettings.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.layout.Pane?>

<Pane xmlns:fx="http://javafx.com/fxml/1" fx:id="content" xmlns="http://javafx.com/javafx/8.0.171"
      fx:controller="control.options.options">
      <RadioButton fx:id="languageGerman" layoutX="274.0" layoutY="114.0" mnemonicParsing="false" text="Deutsch">
         <toggleGroup>
            <ToggleGroup fx:id="language" />
         </toggleGroup></RadioButton>
      <RadioButton fx:id="languageEnglish" layoutX="274.0" layoutY="199.0" mnemonicParsing="false" text="English" toggleGroup="$language" />
      <Button layoutX="758.0" layoutY="406.0" mnemonicParsing="false" onAction="#onLanguageSaveClick" text="Save" />
</Pane>

control.options.options.java

package control.options;

        import java.io.IOException;
        import java.net.URL;
        import java.util.ResourceBundle;

        import constants.FileConstants;
        import javafx.application.Platform;
        import javafx.collections.ObservableList;
        import javafx.event.ActionEvent;
        import javafx.fxml.FXML;
        import javafx.fxml.FXMLLoader;
        import javafx.scene.Parent;
        import javafx.scene.control.Label;
        import javafx.scene.control.RadioButton;
        import javafx.scene.control.Slider;
        import javafx.scene.control.ToggleGroup;
        import javafx.scene.input.MouseEvent;
        import javafx.scene.layout.Pane;
        import view.options.Options;

        import javax.swing.event.TreeModelEvent;

public class options {

    @FXML
    private ResourceBundle resources;
    @FXML
    private URL location;

    @FXML
    private Label label_general;
    @FXML
    private Label label_effects;
    @FXML
    private Label label_music;
    @FXML
    private Pane content;
    @FXML
    private Slider slider_general;
    @FXML
    private Slider slider_effects;
    @FXML
    private Slider slider_music;

    @FXML
    private ToggleGroup language;

    @FXML
    private RadioButton languageGerman;

    @FXML
    private RadioButton languageEnglish;

    @FXML
    void onEffectsChanged(MouseEvent event) {
        int sliderValue = (int) slider_effects.getValue();
        System.out.println(sliderValue + " ");
        label_effects.setText("general (" + slider_effects.getValue()+"%)");
    }

    @FXML
    void onExitClick(ActionEvent event) {
        Platform.exit();
    }

    @FXML
    void onGeneralChanged(MouseEvent event) {
        int sliderValue = (int) slider_general.getValue();
        System.out.println(sliderValue + " ");
        label_general.setText("general (" + slider_general.getValue()+"%)");
    }

    @FXML
    void onGraphicsClick(ActionEvent event) {

    }

    @FXML
    void onLanguageClick(ActionEvent event) throws IOException {
        System.out.println("click! taskBarButton2");
        Parent language = FXMLLoader.load(getClass().getResource("/view/options/languageSettings.fxml"));
        content.getChildren().clear();
        content.getChildren().add(language);
    }

    @FXML
    void onLanguageSaveClick(ActionEvent event) {
        RadioButton selectedRadioButton = (RadioButton) language.getSelectedToggle();
        String toogleGroupValue = selectedRadioButton.getText();
        System.out.println(toogleGroupValue);
    }

    @FXML
    void onMusicChanged(MouseEvent event) {
        int sliderValue = (int) slider_music.getValue();
        System.out.println(sliderValue + " ");
        label_music.setText("general (" + slider_music.getValue()+"%)");
    }

    @FXML
    void onResolutionSaveClick(ActionEvent event) {

    }

    @FXML
    void onSoundClick(ActionEvent event) {

    }

    @FXML
    void initialize() {

    }
}

这是错误日志:

click! taskBarButton2
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$2(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$3(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
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 control.options.options.onLanguageClick(options.java:83)
    ... 58 more
exited

Process finished with exit code 0

options.java:83是content.getChildren().clear();

因此,FXML的加载不是问题。孩子们的清理是。

0 个答案:

没有答案