带场景构建器的JavaFx java.lang.NullPointerException

时间:2015-03-21 09:10:02

标签: netbeans nullpointerexception javafx scenebuilder

我试图制作一个登录或注册人的程序。但我一直得到相同的异常。(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$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:76)
    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/1727479380.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: 1 second)

我已经在这行代码中遇到了很多问题。

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

在我的控制器中:

/*
 * 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.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 ComboBox chTaal;
    @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;

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

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

    }

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

    @FXML
    private void actionchbTaal(ActionEvent event) {
        int keuzeTaal = chTaal.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();
        dc.meldAan(id, ww);
    }

    @FXML
    private void btnRegistreer(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);
    }


    }

这是我的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.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">
  <items>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
         <children>
            <ComboBox fx:id="chTaal" layoutX="43.0" layoutY="18.0" onAction="#actionchbTaal" prefWidth="150.0" />
            <Label layoutX="352.0" text="SOKOBAN" textFill="#d7300f">
               <font>
                  <Font name="Colonna MT" size="48.0" />
               </font>
            </Label>
         </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 layoutX="30.0" layoutY="165.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:

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

这是startUp:

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

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

0 个答案:

没有答案