0

I want to run a JavaFX application with a tableView in it. It worked until I tried to add some data to the tableview... after that I've got following Exception:

    Exception in Application start method
Exception in thread "main" 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$156(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: 
/Users/.../Desktop/ConnectToServer/out/production/ConnectToServer/sample/View/sample.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2571)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
    at sample.MainApp.start(MainApp.java:33)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: 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:497)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2566)
    ... 9 more
Caused by: java.lang.NullPointerException
    at sample.View.Controller.initialize(Controller.java:73)
    ... 19 more

Process finished with exit code 1

I've got following Classes MainApp.java

    package sample;

    import javafx.application.Application;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.GridPane;
    import javafx.stage.Stage;
    import sample.Model.OrderData;
    import sample.Model.OrderDataWrapper;
    import sample.Util.ConnectionToServer;
    import sample.View.Controller;

/**
 * Created by huwiler on 26.02.2016.
 */
public class MainApp extends Application{
    private ObservableList<OrderDataWrapper> list = FXCollections.observableArrayList();

    public ObservableList<OrderDataWrapper> getList() {
        return list;
    }

    private Stage primaryStage;
    private BorderPane rootLayout;

    @Override
    public void start(Stage primaryStage) throws Exception{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class.getResource("View/sample.fxml"));
        GridPane overView = (GridPane) loader.load();

        primaryStage.setTitle("Swiss Domino Effect - Order Panel");
        primaryStage.setScene(new Scene(overView, 694, 457));
        Controller controller = new Controller();
        primaryStage.show();
        list.add(0, createTest());
        controller.setMainApp(this);

    }


    public OrderDataWrapper createTest(){
        OrderData order = new OrderData();
        order.setName("Gustav");
        order.setZahlungsstatus("Offen");
        order.setBearbeitungsstatus("Offen");
        order.setFirma("Huwos");
        order.setAnrede("Herr");
        order.setArticle("500 Dominosteine");
        order.setDate("45.45.4949");
        order.setDescription("Nichts");
        order.setEmail("Daniel@asdfjhkasdf.com");
        order.setId(1);
        order.setStreet("addresss");
        order.setPlace("asdfsdfsdf");
        order.setPhone("82304893204");
        order.setCountry("Schweiz");
        OrderDataWrapper test = new OrderDataWrapper(order);
        return test;
    }

    public MainApp(){
        list.add(createTest());
        list.add(createTest());
        list.add(createTest());
    }

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

Controller

 package sample.View;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import sample.MainApp;
import sample.Model.OrderData;
import sample.Model.OrderDataWrapper;

import java.sql.Date;

public class Controller {
    @FXML
    private Label nameField;
    @FXML
    private Label idField;
    @FXML
    private Label streetField;
    @FXML
    private Label placeField;
    @FXML
    private Label countryField;
    @FXML
    private Label articleField;
    @FXML
    private Label descriptionField;
    @FXML
    private Label emailField;
    @FXML
    private Label phoneField;
    @FXML
    private Label anredeField;
    @FXML
    private Label firmaField;
    @FXML
    private TextField bearbeitungsstatusField;
    @FXML
    private TextField zahlungsstatus;
    @FXML
    private Label date;

    @FXML
    private TableView<OrderDataWrapper> tableView;
    @FXML
    private TableColumn tableColumnId;
    @FXML
    private TableColumn tableColumnDate;
    @FXML
    private TableColumn tableColumnName;

    private MainApp mainApp;

    @FXML
    private void initialize(){
        tableColumnId.setCellValueFactory(new PropertyValueFactory<OrderData, Integer>("id"));


        tableColumnDate.setCellValueFactory(new PropertyValueFactory<OrderData, Date>("date"));
        tableColumnName.setCellValueFactory(new PropertyValueFactory<OrderData, String>("name"));
        //tableView.getColumns().addAll(tableColumnId, tableColumnDate, tableColumnName);
        tableView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<OrderDataWrapper>() {
            @Override
            public void changed(ObservableValue<? extends OrderDataWrapper> observable, OrderDataWrapper oldValue, OrderDataWrapper newValue) {
                refreshInformationPane(newValue);
            }
        });

        tableView.setItems(mainApp.getList());
        tableView.getSelectionModel().selectFirst();

    }


    public void refreshInformationPane(OrderDataWrapper data){
        idField.setText(String.valueOf(data.getId()));
        articleField.setText(data.getArticle());
        date.setText(String.valueOf(data.getDate()));
        anredeField.setText(data.getAnrede());
        nameField.setText(data.getName());
        firmaField.setText(data.getFirma());
        streetField.setText(data.getStreet());
        placeField.setText(data.getPlace());
        countryField.setText(data.getCountry());
        phoneField.setText(data.getPhone());
        emailField.setText(data.getEmail());
        bearbeitungsstatusField.setText(data.getBearbeitungsstatus());
        zahlungsstatus.setText(data.getZahlungsstatus());
        descriptionField.setText(data.getDescription());
    }

    public void setMainApp(MainApp mainApp){
        this.mainApp = mainApp;
    }
}

sample.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>

<GridPane alignment="center" hgap="10" prefHeight="457.0" prefWidth="694.0" vgap="10" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.View.Controller">
   <columnConstraints>
      <ColumnConstraints />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints />
   </rowConstraints>
   <children>
      <BorderPane prefHeight="470.0" prefWidth="701.0">
         <top>
            <MenuBar BorderPane.alignment="CENTER">
              <menus>
                <Menu mnemonicParsing="false" text="Connection">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Connect now" />
                  </items>
                </Menu>
              </menus>
            </MenuBar>
         </top>
         <center>
            <SplitPane blendMode="DARKEN" dividerPositions="0.5" prefHeight="160.0" prefWidth="200.0" BorderPane.alignment="CENTER">
              <items>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                     <children>
                        <TableView fx:id="tableView" cache="true" layoutY="9.0" prefHeight="430.0" prefWidth="343.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                          <columns>
                              <TableColumn fx:id="tableColumnId" editable="false" maxWidth="2500.0" minWidth="20.0" prefWidth="67.0" text="ID" />
                            <TableColumn fx:id="tableColumnDate" editable="false" maxWidth="4000.0" prefWidth="133.0" text="Date" />
                            <TableColumn fx:id="tableColumnName" editable="false" prefWidth="75.0" sortable="false" text="Name" />
                          </columns>
                           <columnResizePolicy>
                              <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                           </columnResizePolicy>
                        </TableView>
                     </children>
                  </AnchorPane>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                     <children>
                        <GridPane layoutX="-70.0" layoutY="12.0" prefHeight="417.0" prefWidth="343.0" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="5.0">
                          <columnConstraints>
                            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                          </columnConstraints>
                          <rowConstraints>
                              <RowConstraints maxHeight="400.0" minHeight="10.0" prefHeight="63.0" vgrow="SOMETIMES" />
                            <RowConstraints maxHeight="400.0" minHeight="10.0" prefHeight="344.0" vgrow="SOMETIMES" />
                            <RowConstraints maxHeight="203.0" minHeight="10.0" prefHeight="36.0" vgrow="SOMETIMES" />
                          </rowConstraints>
                           <children>
                              <GridPane prefHeight="417.0" prefWidth="343.0" GridPane.rowIndex="1">
                                <columnConstraints>
                                  <ColumnConstraints hgrow="SOMETIMES" maxWidth="147.0" minWidth="10.0" prefWidth="97.0" />
                                  <ColumnConstraints hgrow="SOMETIMES" maxWidth="206.0" minWidth="10.0" prefWidth="206.0" />
                                </columnConstraints>
                                <rowConstraints>
                                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                </rowConstraints>
                                 <children>
                                    <Label text="ID" />
                                    <Label text="Article" GridPane.rowIndex="1" />
                                    <Label text="Name" GridPane.rowIndex="4" />
                                    <Label text="Street" GridPane.rowIndex="6" />
                                    <Label text="City" GridPane.rowIndex="7" />
                                    <Label text="Country" GridPane.rowIndex="8" />
                                    <Label text="Phone" GridPane.rowIndex="9" />
                                    <Label text="Email" GridPane.rowIndex="10" />
                                    <Label text="Shipping Status" GridPane.rowIndex="11" />
                                    <Label text="Payment Status" GridPane.rowIndex="12" />
                                    <Label fx:id="idField" text="Label" GridPane.columnIndex="1" />
                                    <Label fx:id="articleField" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="1" />
                                    <Label fx:id="nameField" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="4" />
                                    <Label fx:id="streetField" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="6" />
                                    <Label fx:id="placeField" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="7" />
                                    <Label fx:id="countryField" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="8" />
                                    <Label fx:id="phoneField" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="9" />
                                    <Label fx:id="emailField" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="10" />
                                    <TextField fx:id="bearbeitungsstatusField" prefWidth="50.0" GridPane.columnIndex="1" GridPane.rowIndex="11" />
                                    <TextField fx:id="zahlungsstatus" GridPane.columnIndex="1" GridPane.rowIndex="12" />
                                    <Label text="Salutation" GridPane.rowIndex="3" />
                                    <Label fx:id="anredeField" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="3" />
                                    <Label text="Business" GridPane.rowIndex="5" />
                                    <Label fx:id="firmaField" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="5" />
                                    <Label text="Remark" GridPane.rowIndex="13" />
                                    <Label fx:id="descriptionField" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="13" />
                                    <Label text="Date" GridPane.rowIndex="2" />
                                    <Label fx:id="date" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="2" />
                                 </children>
                              </GridPane>
                              <Label text="Order">
                                 <font>
                                    <Font size="32.0" />
                                 </font>
                              </Label>
                              <GridPane GridPane.rowIndex="2">
                                <columnConstraints>
                                  <ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                  <ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                    <ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                </columnConstraints>
                                <rowConstraints>
                                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                </rowConstraints>
                                 <children>
                                    <Button mnemonicParsing="false" text="Delete Order" />
                                    <Button mnemonicParsing="false" text="Print Order" GridPane.columnIndex="1" />
                                    <Button mnemonicParsing="false" text="Email Order" GridPane.columnIndex="2" />
                                 </children>
                              </GridPane>
                           </children>
                        </GridPane>
                     </children>
                  </AnchorPane>
              </items>
            </SplitPane>
         </center>
      </BorderPane>
   </children>
</GridPane>

Can someone help me? I think the problem is in the initialize section... I'm new into JavaFX.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
DaniSDE
  • 111
  • 1
  • 1
  • 10
  • Is that the full stack trace? It looks like there should be more. If there's more, please include the whole thing. – James_D Feb 26 '16 at 19:07
  • I added the whole thing right now! – DaniSDE Feb 26 '16 at 19:10
  • 1
    It is just a null pointer exception. Read http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it and the linked http://stackoverflow.com/q/3988788/2775450 – James_D Feb 26 '16 at 19:12
  • Okey i got the nullpointer... – DaniSDE Feb 26 '16 at 19:15
  • When should I set the Items to the TableView? In which section? – DaniSDE Feb 26 '16 at 19:16
  • Usually you would do all that in the controller, not in the `Application` class. In a larger application you would have a separate model class. See e.g. http://stackoverflow.com/questions/32342864/applying-mvc-with-javafx – James_D Feb 26 '16 at 19:22

0 Answers0