0

I'm attempting to add data to my TableView but have come across an error. I'm trying to learn how to add random data I make to the table before moving on to using data from a csv file. I followed an online tutorial however I'm getting a null pointer exception error on the line propertyData.setItems(propertyListModel);.

POJO class:

public class PropertyListModel {

    private SimpleStringProperty hostName;
    private SimpleIntegerProperty price;
    private SimpleIntegerProperty minimumNights;
    private SimpleDoubleProperty reviews;


    public PropertyListModel(String hostName, Integer price, Integer minimumNights, Double reviews) {
        this.hostName = new SimpleStringProperty(hostName);
        this.price = new SimpleIntegerProperty(price);
        this.minimumNights = new SimpleIntegerProperty(minimumNights);
        this.reviews = new SimpleDoubleProperty(reviews);

    }

    public String getHostName() {
        return hostName.get();
    }

    public void setHostName(String hostName) {
        this.hostName = new SimpleStringProperty(hostName);
    }

    public int getPrice() {
        return price.get();
    }

    public void setPrice(int price) {
        this.price = new SimpleIntegerProperty(price);
    }

    public int getMinimumNights() {
        return minimumNights.get();
    }

    public void setMinimumNights(int minimumNights) {
        this.minimumNights = new SimpleIntegerProperty(minimumNights);
    }
    public double getReviews() {
        return reviews.get();
    }

    public void setReviews(double reviews) {
        this.reviews = new SimpleDoubleProperty(reviews);
    }
}

Complete stack trace:

java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
    at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: javafx.fxml.LoadException: 
/Users/Amy/Documents/GitHub/PropertyViewer/out/production/PropertyViewer/listings.fxml

    at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2685)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3331)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3287)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3255)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3227)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3203)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3196)
    at Test.start(Test.java:17)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.TableView.setItems(javafx.collections.ObservableList)" because "this.propertyData" is null
    at ListingsController.initialize(ListingsController.java:66)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2655)
    ... 14 more
Exception running application Test```
  • 2
    *"I'm getting a null pointer exception error on the line `propertyData.setItems(propertyListModel);`."* So then `propertyData` is null. Create and post a [mre] and the complete stack trace. – James_D Mar 12 '22 at 21:15
  • The cause of the error is the code you omit. – jewelsea Mar 12 '22 at 21:18
  • @James_D Okay I'll do that now, thanks –  Mar 12 '22 at 21:33
  • @James_D I've edited the post –  Mar 12 '22 at 21:39
  • 1
    That's not a [mre]. Include an FXML file and application class. – James_D Mar 12 '22 at 21:41
  • @James_D Is it correct now? Sorry I'm new to this –  Mar 12 '22 at 22:09
  • 2
    Yes. And the problem is clear, isn't it? The `TableView` has no `fx:id` attribute. – James_D Mar 12 '22 at 22:11
  • @James_D Ahh I don't know how I missed that, thanks! I've now added `fx:id = "propertyData"` and the data for price and reviews is shown however not for host name or minimum nights. –  Mar 12 '22 at 22:18
  • 3
    You shouldn’t create new properties every time a value is set, just set the value in an existing property. If you use PropertyValueFactory you need to follow lookup naming conventions (no spaces, read the PropertyValueFactory javadoc). Prefer using a lambda instead of a PropertyValueFactory. – jewelsea Mar 12 '22 at 22:35
  • @jewelsea How would I go about changing `hostName.setCellValueFactory(new PropertyValueFactory<>("hostName"));` and using a lambda instead? I tried `hostName.setCellValueFactory(cellData ->cellData.getValue().hostNameProperty());` based on other examples I saw online however it didn't work –  Mar 12 '22 at 23:13
  • 1
    Of course not; you don't have a `hostNameProperty()` method. It looks a bit like you are blindly copying code without understanding what it does. You should use the [JavaFX properties pattern](https://docs.oracle.com/javafx/2/binding/jfxpub-binding.htm) in your model class. – James_D Mar 12 '22 at 23:26
  • @James_D Thanks for the link, I understand it now and it's all working :) –  Mar 12 '22 at 23:48

0 Answers0