Ben Gabriëls
Ben Gabriëls

Reputation: 67

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

I'm trying to make a program to Log in or register a person. But I keep getting the same Exception.(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$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)

This is the code from my StartSchermController(the controller from my fxml file)

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

    }

this is the code from my fxml file generated by 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>

And this is the code from my domeinController where the code to set the language set.(The choice for a language is done with 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);
    }
;
}

Thank you in advance. I hope somebody can help me. (sorry for my bad english)

Upvotes: 0

Views: 5023

Answers (1)

OttPrime
OttPrime

Reputation: 1938

Your controller is declaring chTaal as a ChoiceBox but your FXML is declaring it as a ComboBox. The FXMLLoader can't coerce a conversion between those types so it doesn't set chTaal to anything, leaving it as null. Make the types match in the two files and see if that corrects your issue.

Upvotes: 3

Related Questions