0

I am trying to set up a JavaFX GUI Application that (for now) displays a TreeTableView with two Columns. I am using a Main class, Controller class, Product class and an FXML-File to accomplish the whole thing. However it wont start up and i cannot figure out why.

Here is my Controller class:

public class Controller implements Initializable {

    @FXML
    private TreeTableView<Product> tableView;
    @FXML
    TreeTableColumn<Product, String> nameCol;
    @FXML
    TreeTableColumn<Product, String> quantityCol;

    TreeItem<Product> product1 = new TreeItem<>(new Product("Bread", "300g"));
    TreeItem<Product> product2 = new TreeItem<>(new Product("Eggs", "6"));
    TreeItem<Product> product3 = new TreeItem<>(new Product("Coconuts", "4"));
    TreeItem<Product> product4 = new TreeItem<>(new Product("Moisturizer", "2"));
    TreeItem<Product> product5 = new TreeItem<>(new Product("Deodorant", "4"));

    TreeItem<Product> root = new TreeItem<>(new Product("Name", "Quantity"));

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        root.getChildren().setAll(product1, product2, product3, product4, product5);

        nameCol.setCellValueFactory(data -> {
            TreeItem<Product> treeItem = data.getValue();
            Product product = treeItem.getValue();
            return product.nameProperty();
        });
        quantityCol.setCellValueFactory(data -> {
            TreeItem<Product> treeItem = data.getValue();
            Product product = treeItem.getValue();
            return product.quantityProperty();
        });

        tableView.setRoot(root);
        tableView.setShowRoot(false);
    }
}

This is my Main:

public class Main extends Application {

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

    @Override
    public void start (Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Name");
        primaryStage.setScene(new Scene(root, 520, 620));
        primaryStage.show();
    }
}

And this is my Product Class:

public class Product {

    private final StringProperty name = new SimpleStringProperty(this, "name");
    public final void setName(String name) { this.name.set(name); }
    public final String getName() { return name.get(); }
    public final StringProperty nameProperty() { return name; }

    private final StringProperty quantity = new SimpleStringProperty(this, "quantity");
    public final void setQuantity(String quantity) { this.quantity.set(quantity); }
    public final String getQuantity() { return quantity.get(); }
    public final StringProperty quantityProperty() { return quantity; }

    public Product(String name, String quantity) {
        setName(name);
        setQuantity(quantity);
    }
}

Whenever i am trying to launch this whole thing i get the following Exception in start method message displayed:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: javafx.fxml.LoadException: 
/Users/c-mon/IdeaProjects/Guccifer%20copy/out/production/Guccifer/sample/sample.fxml

    at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
    at sample.Main.start(Main.java:17)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
Caused by: java.lang.NullPointerException
    at sample.Controller.initialize(Controller.java:37)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
    ... 14 more
Exception running application sample.Main

I have been trying to make this work for a couple days now and my head is fuming over this.. Is there some obvious flaw in my code that i am missing?

Rebellinho
  • 33
  • 5
  • 1
    One of those columns or `tableView` doesn't seem to be injected. Check, if you've used the correct `fx:id` in `sample.fxml` – fabian Feb 22 '20 at 11:36
  • 1
    I see you renamed your fields based on one of my answers to a previous question of yours. Did you also rename the corresponding `fx:id` attributes in the FXML file? – Slaw Feb 22 '20 at 12:00

0 Answers0