Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PropertyValueFactory error on proper getter from model class

Im trying to populate sample javafx TableView from fxml file.

this is my controller method :

public class TestController implements Initializable {


       @FXML private TableView<user> tableView;
        @FXML private TableColumn<user, String> UserId;
        @FXML private TableColumn<user, String> UserName;


        public void initialize(URL location, ResourceBundle resources) {
            UserId.setCellValueFactory(new PropertyValueFactory<user, String>("userId"));
            UserName.setCellValueFactory(new PropertyValueFactory<user, String>("userName"));


            tableView.getItems().setAll(parseUserList());
        }
        private List<user> parseUserList(){

            List<user> l_u = new ArrayList<user>();

            user u = new user();
            u.setUserId(1);
            u.setUserName("test1");
            l_u.add(u);

            u.setUserId(2);
            u.setUserName("test2");
            l_u.add(u);

            u.setUserId(3);
            u.setUserName("test3");
            l_u.add(u);


            return l_u;
        }

}

and the fxml file :

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="369.0" prefWidth="505.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="viewmodel.TestController ">

   <center>
      <TableView fx:id="tableView" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
        <columns>
          <TableColumn prefWidth="75.0" text="UserId" fx:id="UserId"/>
          <TableColumn prefWidth="75.0" text="UserName" fx:id="UserName"/>
        </columns>
      </TableView>
   </center>
</BorderPane>

and finally my model :

package model;

public class user {
    private int userId;

    public int getUserId() { return this.userId; }
    public void setUserId(int userId) { this.userId = userId; }

    private String userName;

    public String getUserName() { return this.userName; }
    public void setUserName(String userName) { this.userName = userName; }
}

now when i try to populate it gives me this error :

Jun 28, 2019 12:17:35 PM javafx.scene.control.cell.PropertyValueFactory getCellDataReflectively
WARNING: Can not retrieve property 'userId' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory@638db851 with provided class type: class model.user
java.lang.RuntimeException: java.lang.IllegalAccessException: module javafx.base cannot access class model.user (in module JavaFXTest) because module JavaFXTest does not open model to javafx.base
    at javafx.base/com.sun.javafx.property.PropertyReference.get(PropertyReference.java:176)

Some articles on SO mentioned that ,the getter and setter property must be Started with Uppercase after get word but that didn't fix the problem.

like image 919
Ahad Porkar Avatar asked Dec 29 '25 14:12

Ahad Porkar


1 Answers

While violation of java naming conventions (follow them, always!) and improper useage of PropertyValueFactory (don't use it if there is no compelling reason!) are the usual suspects, they seem not to be the reason in the OP's question.

The error message indicates that the problem occurs in a modular context:

Jun 28, 2019 12:17:35 PM javafx.scene.control.cell.PropertyValueFactory getCellDataReflectively
WARNING: Can not retrieve property 'userId' in PropertyValueFactory:   
    javafx.scene.control.cell.PropertyValueFactory@638db851 with provided class type: class model.user
java.lang.RuntimeException: java.lang.IllegalAccessException: 
    module javafx.base cannot access class model.user (in module JavaFXTest) 
    because module JavaFXTest does not open model to javafx.base

Actually, the example runs just fine in a non-modular context but fails when modularized. And the error message tells us exactly what to do: open our module (or parts of it) for reflective access.

Module-info to open either the complete module or individual packages:

 // complete
 open module JavaFXTest {
   exports ourpackage;
   ...
 }

 // parts
 module JavaFXTest {
   exports ourpackage;
   opens ourpackage.model;
 }
like image 51
kleopatra Avatar answered Jan 01 '26 03:01

kleopatra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!