0

I'm trying to learn how TableView work's in JavaFX. I wrote a simple class to add a list of movies to a table but when I run it I'm getting this error:

 javafx.scene.control.cell.PropertyValueFactory getCellDataReflectively
WARNING: Can not retrieve property 'Price' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory@3aebd1d5 with provided class type: class application.MovieClass
java.lang.IllegalStateException: Cannot read from unreadable property Price

there are 3 variables in the movie class and I get the same error for all of them.

here are the 2 classes that I wrote

main:

package application;    

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;


public class Main extends Application {
    TableView<MovieClass> table;
    @Override
    public void start(Stage primaryStage) {
        Label lblHeading = new Label("Movie Inventory"); 
        TableView<MovieClass> table = new TableView<MovieClass>();
        table.setItems(loadData()); 
        TableColumn<MovieClass, String> colTitle = new TableColumn<MovieClass, String>("Title");
        colTitle.setMinWidth(300);
        colTitle.setCellValueFactory(
        new PropertyValueFactory<MovieClass, String>("title"));

        TableColumn<MovieClass, Integer> colYear = new TableColumn<MovieClass, Integer>("year"); 
        colYear.setMinWidth(100);
        colYear.setCellValueFactory(
        new PropertyValueFactory<MovieClass, Integer>("Year"));

        TableColumn<MovieClass, Double> colPrice = new TableColumn<MovieClass, Double>("price"); 
        colPrice.setMinWidth(100);
        colPrice.setCellValueFactory(
        new PropertyValueFactory<MovieClass, Double>("Price"));

        table.getColumns().addAll(colTitle, colYear, colPrice);

        print(loadData());
        VBox paneMain = new VBox();
        paneMain.setSpacing(10);
        paneMain.setPadding(new Insets(10, 10, 10, 10));
        paneMain.getChildren().addAll(lblHeading, table);
        Scene scene = new Scene(paneMain);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Movie Inventory");
        primaryStage.show();
    }


    public ObservableList<MovieClass> loadData() 
    {
        ObservableList<MovieClass> data = FXCollections.observableArrayList();
        data.add(new MovieClass("It's a Wonderful Life",1946, 14.95));
        data.add(new MovieClass("Young Frankenstein",1974, 16.95));
        data.add(new MovieClass("Star Wars Episode 4",1976, 17.95));
        data.add(new MovieClass("The Princess Bride",1987, 16.95));
        data.add(new MovieClass("Glory",1989, 14.95));
        data.add(new MovieClass("The Game",1997, 14.95));
        data.add(new MovieClass("Shakespeare in Love",1998, 19.95));
        data.add(new MovieClass("The Invention of Lying",2009, 18.95));
        data.add(new MovieClass("The King's Speech",2010, 19.95));
        return data; }

    public void print(ObservableList<MovieClass> list) {
        for(int i = 0; i > list.size(); i++) {
            System.out.println("The Title = " + list.get(i).getMyTitle()
                    + "       " + "The Year = " + list.get(i).getMyYear()
                    + "       " + "The Price = " + list.get(i).getMyPrice());
        }
    }

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

movie class:

package application;

public class MovieClass {
    private String title;
    private int year;
    private double price;
    public MovieClass()
    {
        this.title = "";
        this.year = 0;
        this.price = 0.0;
    }
    public MovieClass(String title, int year, double price) 
    {
        this.title = title;
        this.year = year;
        this.price = price;
    }

    public String getTitle() {
        return this.title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public int getYear() {
        return this.year;
    }
    public void setYear(int year) {
        this.year = year;
    }
    public double getPrice() {
        return this.price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
}
fabian
  • 80,457
  • 12
  • 86
  • 114
  • 1
    I cannot reproduce this error. However your `print` method does not compile since the methods you invoke in the method body are not part of the `MovieClass` class. Are your sure you didn't change the the name of the methods/the constructor paramerters for `PropertyValueFactory` and recompilation failed afterwards? – fabian Jun 26 '18 at 16:05
  • No, I didn't change anything after. So you were able to run it with no error?? – rayan mayson Jun 26 '18 at 16:30
  • Indeed, that is after I commented out the whole content of the `print` method body. You're not using java >= 9 btw? Otherwise you need to make sure the module allows reflective access to the method. – fabian Jun 26 '18 at 16:37
  • DUPLICATE https://stackoverflow.com/questions/46878130/javafx-tableview-cannot-read-from-unreadable-property – SlashUzero Feb 06 '19 at 08:56

0 Answers0