JavaFX java.lang.reflect.InvocationTargetException(带场景生成器)

JavaFX java.lang.reflect.InvocationTargetException (with scene builder)

我正在尝试制作一个程序来登录或注册一个人。 但我不断收到相同的异常。(InvocationTargetException)

Exception in Application start method
file:/C:/Users/Eigenaar/Desktop/p1g04/dist/SokobanGroep4.jar!/gui/StartScherm.fxml:19

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:483)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:363)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
    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:483)
    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:875)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication7(LauncherImpl.java:157)
    at com.sun.javafx.application.LauncherImpl$$Lambda/1099983479.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at gui.StartSchermController.<init>(StartSchermController.java:73)
    at StartUp.start(StartUp.java:27)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication13(LauncherImpl.java:821)
    at com.sun.javafx.application.LauncherImpl$$Lambda/2006606717.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(PlatformImpl.java:323)
    at com.sun.javafx.application.PlatformImpl$$Lambda/128893786.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null4(PlatformImpl.java:292)
    at com.sun.javafx.application.PlatformImpl$$Lambda/2063964656.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater5(PlatformImpl.java:291)
    at com.sun.javafx.application.PlatformImpl$$Lambda/1108411398.run(Unknown Source)
    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$null1(WinApplication.java:102)
    at com.sun.glass.ui.win.WinApplication$$Lambda/1147985808.run(Unknown Source)
    ... 1 more
Exception running application StartUp
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

这是我的 StartSchermController(我的 fxml 文件中的控制器)的代码

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import domein.DomeinController;
import java.io.IOException;
import java.util.ArrayList;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TextField;

/**
 * FXML Controller class
 *
 * @author Eigenaar
 */
public class StartSchermController extends SplitPane {

    @FXML
    private ChoiceBox chbTaal;
    @FXML
    private TextField txfMeldID;
    @FXML
    private TextField txfMeldWw;
    @FXML
    private TextField txfRegNaam;
    @FXML
    private TextField txfRegVNaam;
    @FXML
    private TextField txfRegWw2;
    @FXML
    private TextField txfRegWw;
    @FXML
    private TextField txfRegID;
   @FXML
    private Label lblMeldAan;
   @FXML
    private Label lblRegistreer;
     @FXML
    private Label lblMeldId;
      @FXML
    private Label lblMeldWw;
       @FXML
    private Label lblRegNaam,lblRegVNaam,lblRegId,lblRegWw,lblRegWw2;

    private final DomeinController dc;

    /**
     * Initializes the controller class.
     */
    public StartSchermController(DomeinController dc) {
               this.dc=dc;

        FXMLLoader loader=new FXMLLoader(getClass().getResource("StartScherm.fxml"));

        loader.setRoot(this);
        loader.setController(this);

        try {
            loader.load();
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
        try{
        chbTaal.setItems(FXCollections.observableArrayList(dc.keuzeTaal()));
        } catch(NullPointerException e){
           System.out.println(e.getMessage()); 
        }



    }

    public int geefKeuze(){
        int s = chbTaal.getSelectionModel().getSelectedIndex();
        return s;
    }

    @FXML
    private void actionchbTaal(ActionEvent event) {
        int keuzeTaal = chbTaal.getSelectionModel().getSelectedIndex();
        update(dc.setTaal(keuzeTaal));
        this.geefKeuze();
    }

    private void update(ArrayList<String> s) {
        lblMeldAan.setText(s.get(0));
        this.lblMeldId.setText(s.get(1));
        this.lblMeldWw.setText(s.get(2));
        this.lblRegistreer.setText(s.get(3));
        this.lblRegNaam.setText(s.get(4));
        this.lblRegVNaam.setText(s.get(5));
        this.lblRegId.setText(s.get(6));
        this.lblRegWw.setText(s.get(7));
        this.lblRegWw2.setText(s.get(8));
    }

    }

这是我的 fxml 文件中由 JavaFX Scene Builder 2.0 生成的代码

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

<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<fx:root dividerPositions="0.19095477386934673" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" orientation="VERTICAL" prefHeight="400.0" prefWidth="600.0" type="SplitPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
  <items>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="148.0" prefWidth="598.0">
         <children>
            <Label layoutX="384.0" layoutY="21.0" text="SOKOBAN" textFill="#d7300f">
               <font>
                  <Font name="Colonna MT" size="24.0" />
               </font>
            </Label>
            <ComboBox fx:id="chbTaal" layoutX="37.0" layoutY="24.0" onAction="#actionchbTaal" prefWidth="150.0" />
         </children></AnchorPane>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
         <children>
            <SplitPane dividerPositions="0.481421647819063" layoutY="-6.0" prefHeight="331.0" prefWidth="621.0">
              <items>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                     <children>
                        <Label fx:id="lblMeldAan" layoutX="12.0" layoutY="1.0" prefHeight="27.0" prefWidth="106.0">
                           <font>
                              <Font size="18.0" />
                           </font>
                        </Label>
                        <Label fx:id="lblMeldId" layoutX="14.0" layoutY="42.0" prefHeight="17.0" prefWidth="74.0" />
                        <TextField fx:id="txfMeldID" layoutX="97.0" layoutY="38.0" />
                        <Label fx:id="lblMeldWw" layoutX="14.0" layoutY="83.0" prefHeight="17.0" prefWidth="74.0" />
                        <TextField fx:id="txfMeldWw" layoutX="97.0" layoutY="79.0" />
                        <TextField layoutX="30.0" layoutY="165.0" prefHeight="116.0" prefWidth="244.0" />
                     </children>
                  </AnchorPane>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                     <children>
                        <Label fx:id="lblRegistreer" layoutX="14.0" layoutY="1.0" prefHeight="27.0" prefWidth="106.0">
                           <font>
                              <Font size="18.0" />
                           </font>
                        </Label>
                        <Label fx:id="lblRegNaam" layoutX="82.0" layoutY="41.0" text="Naam:" />
                        <Label fx:id="lblRegWw" layoutX="51.0" layoutY="148.0" text="Wachtwoord:" />
                        <Label fx:id="lblRegId" layoutX="51.0" layoutY="112.0" text="GebruikersID:" />
                        <Label fx:id="lblRegVNaam" layoutX="58.0" layoutY="76.0" text="Voornaam:" />
                        <Label fx:id="lblRegWw2" layoutX="8.0" layoutY="185.0" text="Controle Wachtwoord:" />
                        <TextField fx:id="txfRegNaam" layoutX="128.0" layoutY="37.0" />
                        <TextField fx:id="txfRegVNaam" layoutX="128.0" layoutY="72.0" />
                        <TextField fx:id="txfRegWw2" layoutX="128.0" layoutY="181.0" />
                        <TextField fx:id="txfRegWw" layoutX="128.0" layoutY="144.0" />
                        <TextField fx:id="txfRegID" layoutX="128.0" layoutY="108.0" />
                     </children>
                  </AnchorPane>
              </items>
            </SplitPane>
         </children></AnchorPane>
  </items>
</fx:root>

这是来自我的 domeinController 的代码,其中设置语言集的代码。(语言的选择是通过 resourcebundels 完成的)

import java.util.ArrayList;
import java.util.Locale;
import java.util.ResourceBundle;

public class DomeinController {

public ArrayList<String> keuzeTaal(){
    ArrayList<String> taal = new ArrayList<>();
    taal.add("Nederlands");
    taal.add("Français");
    taal.add("English");
    return taal;
}

    public ArrayList<String> setTaal(int i){
        ResourceBundle taal=Taal(i);
        ArrayList<String> s= new ArrayList<>();
        s.add(taal.getString("meldAan"));
        s.add(taal.getString("gebruikersID"));
        s.add(taal.getString("wachtwoord"));

        s.add(taal.getString("registreer"));
        s.add(taal.getString("naam"));
        s.add(taal.getString("voornaam"));
        s.add(taal.getString("gebruikersID"));
        s.add(taal.getString("wachtwoord"));
        s.add(taal.getString("herhalingWachtwoord"));
        return s;
    }


 public static ResourceBundle Taal(int taalKeuze) {
        Locale locale;
        ResourceBundle taal;
        if (taalKeuze==1) {
            locale = Locale.FRENCH;
        } else if (taalKeuze==2) {
            locale = Locale.ENGLISH;
        } else {
            locale = new Locale("nl");
        }
        return ResourceBundle.getBundle("taal\LabelsBundle", locale);
    }
;
}

提前谢谢你。我希望有人能帮助我。 (抱歉我的英语不好)

您的控制器将 chTaal 声明为 ChoiceBox,但您的 FXML 将其声明为 ComboBoxFXMLLoader 无法强制在这些类型之间进行转换,因此它不会将 chTaal 设置为任何值,而是将其保留为 null。使两个文件中的类型匹配,看看是否能解决您的问题。