# JavaFX InvocationTargetException



## Sellixx (17. Mai 2016)

Hallo miteinander,

ich bekomme beim starten dieses JavaFx Programms folgende Fehlermeldung.
Da ich sehr neu bei JavaFx bin und auch bei Google nichts passendes fand, bin ich nun hier und bitte darum sich folgendes mal anzuschauen. Ich werde daraus leider nicht schlau.

```
Executing C:\Users\danie\Documents\NetBeansProjects\JavaFXApplication3\dist\run1889442468\JavaFXApplication3.jar using platform C:\Program Files (x86)\Java\jdk1.8.0_74\jre/bin/java
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:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    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:498)
    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:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException:
file:/C:/Users/danie/Documents/NetBeansProjects/JavaFXApplication3/dist/run1889442468/JavaFXApplication3.jar!/javafxapplication3/FXMLDocument.fxml:10

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at javafxapplication3.JavaFXApplication3.start(JavaFXApplication3.java:22)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    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$148(WinApplication.java:191)
    ... 1 more
Caused by: java.lang.NullPointerException
    at javafxapplication3.FXMLDocumentController.<init>(FXMLDocumentController.java:54)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
    at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    ... 17 more
Exception running application javafxapplication3.JavaFXApplication3
Java Result: 1
```

Hier die Main:

```
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class JavaFXApplication3 extends Application {
  
    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
      
        Scene scene = new Scene(root);
      
        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
  
}
```

Controller:

```
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.MenuBar;
import javafx.scene.control.SingleSelectionModel;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

/**
*
* @author danie
*/
public class FXMLDocumentController implements Initializable {
  
    @FXML
    private TextField tf1;
  
    @FXML
    private Label lb1;
  
    @FXML
    private ComboBox box1;
  
    @FXML
    private Button b1;
  
    @FXML
    private TabPane tb;
    @FXML
    private Tab main;
     @FXML
    private Tab kasse;
      @FXML
    private Tab spind;
       @FXML
    private Tab cafe;
        @FXML
    private Tab angestellter;
     
    SingleSelectionModel<Tab> selectionModel = tb.getSelectionModel();
  
    @FXML
    private MenuBar mb;
  
  
  
    @FXML
    private void handleButtonAction(ActionEvent event){
        if (box1.getValue().equals("Kasse")) {
            selectionModel.select(kasse);
        }
  
    }
  
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }  
  
}
```

FXMLDokument:

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

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

<AnchorPane id="AnchorPane" prefHeight="383.0" prefWidth="585.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="javafxapplication3.FXMLDocumentController">
  <children>
    <TabPane fx:id="tb" layoutX="0.0" layoutY="31.0" prefHeight="352.0" prefWidth="585.0" tabClosingPolicy="UNAVAILABLE">
      <tabs>
        <Tab fx:id="main" text="Main">
          <content>
            <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
              <children>
                <ComboBox fx:id="box1" layoutX="224.0" layoutY="103.0" prefHeight="21.0" prefWidth="137.0" promptText="Auswählen...">
                  <items>
                    <FXCollections fx:factory="observableArrayList">
                      <String fx:value="Kasse" />
                      <String fx:value="Spind" />
                      <String fx:value="Cafeteria" />
                      <String fx:value="Angestellter" />
                    </FXCollections>
                  </items>
                </ComboBox>
                <TextField fx:id="tf1" layoutX="193.0" layoutY="186.0" prefWidth="200.0" />
                <Label fx:id="lb1" layoutX="288.0" layoutY="153.0" text="IP" />
                <Button fx:id="b1" layoutX="260.0" layoutY="244.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Connect" />
              </children>
            </AnchorPane>
          </content>
        </Tab>
        <Tab fx:id="kasse" disable="true" text="Kasse">
          <content>
            <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
          </content>
        </Tab>
        <Tab fx:id="spind" disable="true" text="Spind">
          <content>
            <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
          </content>
        </Tab>
        <Tab fx:id="cafe" disable="true" text="Cafeteria">
          <content>
            <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
          </content>
        </Tab>
        <Tab fx:id="angestellter" disable="true" text="Angestellter">
          <content>
            <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
          </content>
        </Tab>
      </tabs>
    </TabPane>
    <MenuBar fx:id="mb" layoutX="0.0" layoutY="0.0" prefHeight="31.000099999997474" prefWidth="585.0" useSystemMenuBar="true">
      <menus>
        <Menu mnemonicParsing="false" text="File">
          <items>
            <MenuItem mnemonicParsing="false" text="Close" />
          </items>
        </Menu>
        <Menu mnemonicParsing="false" text="Edit">
          <items>
            <MenuItem mnemonicParsing="false" text="Delete" />
          </items>
        </Menu>
        <Menu mnemonicParsing="false" text="Help">
          <items>
            <MenuItem mnemonicParsing="false" text="About" />
          </items>
        </Menu>
      </menus>
    </MenuBar>
  </children>
</AnchorPane>
```


----------



## Joose (17. Mai 2016)

Sellixx hat gesagt.:


> Hallo miteinander,
> 
> ich bekomme beim starten dieses JavaFx Programms folgende Fehlermeldung.
> 
> ...



Der interessante Teil vom StackTrace ist diese NullPointer Exception. Und wie dem StackTrace zu entnehmen ist fliegt diese Exception im FXMLDocumentController Zeile 54.
Kurz nachgeschaut nehme ich an das `tb` noch nicht initialisiert ist (null ist), daher erzeugt der Aufruf von `getSelectionModel()` die NPE.


----------



## Sellixx (17. Mai 2016)

Vielen Dank fürs Anschauen. Hast du eine Idee wie ich das lösen könnte ? Als was soll ich das TabPane tb initialisieren ?


----------



## Sellixx (17. Mai 2016)

Okay habe es gelöst.
Ich habe 

```
SingleSelectionModel<Tab> selectionModel = tb.getSelectionModel();
```
entfernt und in der handleButtonAction-methode durch

```
tb.getSelectionModel().select(kasse);
```
ersetzt.

Vielen Dank für die hilfe.


----------

