-1

So I checked the other No content in TableView but it was no help.

I have a database named ledger and I want to bring my transactions into view.

void buildData(){

    final ObservableList<ObservableList<String>> data = null

    try{
        //ResultSet
        ResultSet rs = sql.getTransactions(account.getValue().toString())

        /**********************************
         * TABLE COLUMN ADDED DYNAMICALLY *
         **********************************/
        for(int i=0 ; i<rs.getMetaData().getColumnCount(); i++){
            //We are using non property style for making dynamic table
            final int j = i
            TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i+1))
            col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList,String>,ObservableValue<String>>(){
                ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList, String> param) {
                    return new SimpleStringProperty(param.getValue().get(j).toString())
                }
            })

            budgetTable.getColumns().addAll(col)
        }

        /********************************
         * Data added to ObservableList *
         ********************************/
        while(rs.next()){
            //Iterate Row

            ObservableList<String> row = FXCollections.observableArrayList()
            for(int i=1 ; i<=rs.getMetaData().getColumnCount(); i++){
                //Iterate Column
                row.add(rs.getString(i))
            }
            println("Row [1] added "+row )
            data?.add(row)
        }

        //FINALLY ADDED TO TableView
        budgetTable.setItems(data)
    }catch(Exception e){
        e.printStackTrace()
        System.out.println("Error on Building Data")
    }
}

I have 5 columns in the database that do comeback and are added to the tableview. These are date, from_account, to_account, amount & notes:

mysql> show columns from ledger;
+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+--------------+-------------+------+-----+---------+----------------+
| id           | bigint(20)  | NO   | PRI | NULL    | auto_increment |
| version      | bigint(20)  | NO   |     | 1       |                |
| date         | datetime    | NO   |     | NULL    |                |
| notes        | varchar(35) | NO   |     | NULL    |                |
| amount       | double      | NO   |     | NULL    |                |
| from_account | varchar(19) | NO   |     | NULL    |                |
| to_account   | varchar(55) | YES  |     | NULL    |                |
+--------------+-------------+------+-----+---------+----------------+
7 rows in set (0.45 sec)

I get no error or otherwise I would have a very good chance of solving it. At this point I don't know what the problem is. Just says "No content in table" upon build. The file is a groovy file so that's why it looks like python syntax.

Thank you in advance for your help, time and insights! Be well!

fabian
  • 80,457
  • 12
  • 86
  • 114

1 Answers1

0

I'm not very familiar with Groovy but I believe I know what the issue is. First off, you declare data as final and then assign null to it.

final ObservableList<ObservableList<String>> data = null;

This means it will be null when to go to set the items of the TableView. Basically, you're calling budgetTable.setItems(null). You would not normally reach this point because calling data.add(row) would throw a NullPointerException; except you don't use data.add(row) but rather data?.add(row). The ? here is the safe navigation operator.

The Safe Navigation operator is used to avoid a NullPointerException. Typically when you have a reference to an object you might need to verify that it is not null before accessing methods or properties of the object. To avoid this, the safe navigation operator will simply return null instead of throwing an exception...

See this question for more.

Given all this, simply changing:

final ObservableList<ObservableList<String>> data = null;

To:

final ObservableList<ObservableList<String>> data = FXCollections.observableArrayList();

Should solve your problem.

Slaw
  • 37,820
  • 8
  • 53
  • 80
  • Indeed that was the issue! Thank you so much. Amazing that I looked over that code a number of times and never noticed. I had to use the null safe operator bc I was given a null pointer exception and now I know why! – Philip Caputo Sep 30 '18 at 14:06