JavaFX java.lang.reflect.InitationTargetException(使用场景构建器)

标签 java netbeans javafx scenebuilder invocationtargetexception

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

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$launchApplication$147(LauncherImpl.java:157)
    at com.sun.javafx.application.LauncherImpl$$Lambda$48/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$launchApplication1$153(LauncherImpl.java:821)
    at com.sun.javafx.application.LauncherImpl$$Lambda$51/2006606717.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
    at com.sun.javafx.application.PlatformImpl$$Lambda$45/128893786.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/2063964656.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
    at com.sun.javafx.application.PlatformImpl$$Lambda$46/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$null$141(WinApplication.java:102)
    at com.sun.glass.ui.win.WinApplication$$Lambda$37/1147985808.run(Unknown Source)
    ... 1 more
Exception running application StartUp
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

这是我的 StartSchemController 中的代码(来 self 的 fxml 文件的 Controller )

/*
 * 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));
    }

    }

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

<?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 中的代码,其中设置语言集的代码。(语言的选择是通过资源包完成的)

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

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

最佳答案

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

关于JavaFX java.lang.reflect.InitationTargetException(使用场景构建器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29171765/

相关文章:

java - 在使用 Gradle 构建的 Java 项目中管理 javaagent 依赖项

java - 为什么我会收到 java.lang.NumberFormatException : For input string: "∞"?

java - 始终等待页面加载到 PageObjects 上

java - 查找是如何工作的?

JavaFX : Getting back to main page without FXML

java - 调整 javafx 形状位置的大小而不缩放它们

java - 使用java中Vector的indexOf()方法来匹配一个成员变量

java - Netbeans 中的 Jtable header 大小

java - 错误 - 代码在 NetBeans 中有效,但在 .jar 命令行中无效

java - FXML 文件中的 getHostServices().showDocument()