带有场景生成器 2.0 的 JavaFx java.lang.NullPointerException

JavaFx java.lang.NullPointerException with scene builder 2.0

我正在尝试制作一个程序来登录或注册一个人。但是我得到了异常。(InvocationTargetException 和 NullPointerException

错误堆栈跟踪

Exception in Application start method
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:81)
    at StartUp.start(StartUp.java:27)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication13(LauncherImpl.java:821)
    at com.sun.javafx.application.LauncherImpl$$Lambda/93724165.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: 1 second)

我对这行有问题。

  chTaal.setItems(FXCollections.observableArrayList(dc.keuzeTaal()));

StartSchermController.java

/*
 * 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.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
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 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;
    @FXML
    private Button btnMeldAan,btnRegistreer;
    private final DomeinController dc;
    @FXML
    private ComboBox cmbTaal;
    @FXML
    private TextField txfStartExc;

    /**
     * Initializes the controller class.
     * @param dc
     */
    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());
        }

       cmbTaal.setItems(FXCollections.observableArrayList(dc.keuzeTaal()));

    }

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

    @FXML
    private void cmbTaalOnAction(ActionEvent event) {
        int keuzeTaal = cmbTaal.getSelectionModel().getSelectedIndex();
        System.out.println(keuzeTaal);
        updateTaal(dc.setTaal(keuzeTaal));
        this.geefKeuze();
    }

    private void updateTaal(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
    private void btnMeldAanOnAction(ActionEvent event){
       String id= this.txfMeldID.getText();
       String ww= this.txfMeldWw.getText();
       try{
       dc.meldAan(id, ww);
       }catch(Exception e){
           txfStartExc.setText(e.getMessage());
           this.txfMeldID.setText("");
           this.txfMeldWw.setText("");
       }
       this.txfMeldID.setText("");
       this.txfMeldWw.setText("");
    }

    @FXML
    private void btnRegistreerOnAction(ActionEvent event){
       String n=this.txfRegNaam.getText();
       String vn=this.txfRegVNaam.getText();
       String id= this.txfRegID.getText();
       String ww= this.txfRegWw.getText();
       String ww2=this.txfRegWw2.getText();
        dc.maakSpelerAan(n, vn, id, ww);
    }


}

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

StartScherm.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.17839195979899497" 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" fx:controller="gui.StartSchermController">
  <items>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
         <children>
            <Label layoutX="352.0" text="SOKOBAN" textFill="#d7300f">
               <font>
                  <Font name="Colonna MT" size="48.0" />
               </font>
            </Label>
            <ComboBox fx:id="cmbTaal" layoutX="46.0" layoutY="18.0" onAction="#cmbTaalOnAction" prefWidth="150.0" />
         </children></AnchorPane>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="257.0" prefWidth="598.0">
         <children>
            <SplitPane dividerPositions="0.481421647819063" layoutY="-8.0" prefHeight="400.0" prefWidth="621.0">
              <items>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="346.0" prefWidth="295.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 fx:id="txfStartExc" layoutX="26.0" layoutY="184.0" prefHeight="116.0" prefWidth="244.0" />
                        <Button fx:id="btnMeldAan" layoutX="97.0" layoutY="129.0" mnemonicParsing="false" onAction="#btnMeldAanOnAction" text="Meld aan" />
                     </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="8.0" layoutY="41.0" prefHeight="17.0" prefWidth="113.0" text="" />
                        <Label fx:id="lblRegWw" layoutX="8.0" layoutY="148.0" prefHeight="17.0" prefWidth="113.0" text="" />
                        <Label fx:id="lblRegId" layoutX="8.0" layoutY="112.0" prefHeight="17.0" prefWidth="113.0" text="" />
                        <Label fx:id="lblRegVNaam" layoutX="8.0" layoutY="76.0" prefHeight="17.0" prefWidth="113.0" text="" />
                        <Label fx:id="lblRegWw2" layoutX="8.0" layoutY="185.0" text="" />
                        <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" />
                        <Button fx:id="btnRegistreer" layoutX="128.0" layoutY="226.0" mnemonicParsing="false" onAction="#btnRegistreerOnAction" text="Registreer" />
                     </children>
                  </AnchorPane>
              </items>
            </SplitPane>
         </children></AnchorPane>
  </items>
</fx:root>

DomeinController.java

package domein;

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

public class DomeinController {

    private final SpelerRepository spelerRepository;
    private Speler speler;
   // private Spel spel;

    public DomeinController() {
        spelerRepository = new SpelerRepository();
    }

/*
    public void registreer(String naam, String voornaam, String email, LocalDate geboortedatum, String wachtwoord, String wachtwoordBevestiging) {
        if (!wachtwoord.equals(wachtwoordBevestiging)) {
            throw new EmailException();
        }*/
/*
        Speler nieuweSpeler = new Speler(naam, voornaam, gebruikersID, wachtwoord);
        setSpeler(nieuweSpeler); // ONTBREEKT!!
        spelerRepository.voegToe(nieuweSpeler);

    }*/

    /** UC_1 meldAan DOING */
    public void meldAan(String gebruikersID, String wachtwoord) {
        Speler gevondenSpeler = spelerRepository.meldAan(gebruikersID, wachtwoord); 
        if (gevondenSpeler != null) {
            setSpeler(gevondenSpeler);
        }
    }


   /** UC_1 meldAan KNOWING */
    public String[] geefDetailsSpeler() {
       return speler.geefDetailsSpeler();
        /* if (speler == null) {
            return null;
        }

        String[] spelerS = new String[3];
        spelerS[0] = speler.getVoornaam();
        spelerS[1] = speler.getNaam();
        return spelerS;*/
    }

    private void setSpeler(Speler speler){
        this.speler = speler;
    }

    public void maakSpelerAan(String naam, String voornaam, String gebruikersID, String wachtwoord) {
         Speler s=new Speler(naam,voornaam,gebruikersID,wachtwoord,false);

             spelerRepository.voegToe(s);
    }

    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=setResourceBundle(i);
        ArrayList<String> s= new ArrayList<>();
        s.add(taal.getString("meldAan"));
        s.add(taal.getString("userID"));
        s.add(taal.getString("wachtwoord"));

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


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

/*
    private void setSpel(Spel spel) {
        this.spel = spel;
    }
*/

主要ClassStartUp.java

import domein.DomeinController;
import gui.StartSchermController;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.stage.Stage;

/*
 * 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.
 */

/**
 *
 * @author Eigenaar
 */
@SuppressWarnings("unchecked")
public class StartUp extends Application
{
    @Override
    @SuppressWarnings("unchecked")
    public void start(Stage stage)
    {
        DomeinController controller = new DomeinController();
        StartSchermController root = new StartSchermController(controller);
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.setTitle("Startscherm Sokoban");
        stage.show();
    }

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

提前谢谢你。我希望有人能帮助我。

我认为您的 FXML 未加载。调用 loader.load(); 检查后,将 System.exit(1); 添加到 catch 块。

如果您使用 loader.setController(...),则 FXML 文件中不应包含 fx:controller 属性。删除该属性,它应该可以解决问题。