johnsnow333
johnsnow333

Reputation: 9

issue with this tutorial:Populate a tableview using database in JavaFX

I get a nullpointerxception when following this tutorial: Populate a tableview using database in JavaFX .

I modified it to make it simpler and fit my needs: Instead of Usermaster, I have Person object.

 while(rs.next()){
        Person per = new Person();
        per.ClientID.set(rs.getInt(1));
        per.FirstName.set(rs.getString(2));
        per.LastName.set(rs.getString(3));

The code stops at per.ClientID.set(rs.getInt(1)); due to nullpointerxception.

If I make system.out.println(rs.getInt(1)) (or any other column), I get the value... But it appears that I can't pass it to my object per.

All Person object vars are SimpleString/IntergerProperty type, as shown in the tutorial.

Can someone help me to identify the mistake I made in coding this?

Thank you

**Answer: need to initialize values.

Now I have no errors, but my table is not populating...

Full code:

a) Main App

package tableview;


import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MainApp extends Application {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("view/FXMLTable.fxml"));
    Scene scene = new Scene(root);
    primaryStage.setScene(scene);
    primaryStage.show();    
}

}

Model Class:

package tableview.model;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Person {
    public SimpleIntegerProperty ClientID = new SimpleIntegerProperty();
    public SimpleStringProperty FirstName = new SimpleStringProperty();
    public SimpleStringProperty LastName = new SimpleStringProperty();

    public SimpleIntegerProperty getClientID() {
        return ClientID;
    }
    public SimpleStringProperty getFirstname() {
        return FirstName;
    }
    public SimpleStringProperty getLastName() {
        return LastName;
    }
    public IntegerProperty clientIDProperty(){
        return ClientID;
    }
    public StringProperty firstNameProperty(){
        return FirstName;

    }
    public StringProperty lastNameProperty(){
        return LastName;    
    }
}

Controller Class:

package tableview.view;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import tableview.model.Person;


public class FXMLTableController{

@FXML
public TableView<Person> tableview ;
@FXML
private TableColumn<Person, Number> clientIdColumn;
@FXML
private TableColumn<Person, String> firstNameColumn;
@FXML
private TableColumn<Person, String> lastNameColumn;

@FXML   
private void initialize() {
         assert tableview != null : "fx:id=\"tableview\" was not injected: check your FXML file 'UserMaster.fxml'.";
         clientIdColumn.setCellValueFactory(cellData -> cellData.getValue().
                 clientIDProperty());

        firstNameColumn.setCellValueFactory(cellData -> cellData.getValue()
                .firstNameProperty());              
        lastNameColumn.setCellValueFactory(cellData -> cellData.getValue()
                .lastNameProperty());

         buildData();

    }

private ObservableList<Person> data;


public  void buildData(){        
    data = FXCollections.observableArrayList();
    Connection con = null;


        try {
            Class.forName("org.sqlite.JDBC");
            con = DriverManager.getConnection("jdbc:sqlite:tableviewdb.db");

        String SQL = "Select * from INFO";            
        ResultSet rs = con.createStatement().executeQuery(SQL);  
        while(rs.next()){
            Person per = new Person();
            per.ClientID.set(rs.getInt("CLIENTID"));
            per.FirstName.set(rs.getString("FIRSTNAME"));
            per.LastName.set(rs.getString("LASTNAME"));

            data.add(per);  

        }
        tableview = new TableView<Person>();
        tableview.setItems(data);
        System.out.println(tableview.getItems().get(1).ClientID);
    }
    catch(Exception e){
          e.printStackTrace();
          System.out.println("Error on Building Data");            
    }
   }
}

Upvotes: 0

Views: 888

Answers (1)

Roland
Roland

Reputation: 18425

ClientID is null. You didn't initialize it.

If it's a property, you should create the proper getter and setters for it and not use the property directly. Besides you should never use 1, 2, etc in the ResultSet's getter. It's better practice to use the column names.

Upvotes: 2

Related Questions