0

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");            
    }
   }
}
Community
  • 1
  • 1
johnsnow333
  • 9
  • 1
  • 5
  • If you don't show the full code with the stacktrace you are wasting everyone's time. Also, you may want to read this: http://blog.netopyr.com/2011/05/19/creating-javafx-properties/ – Roland Mar 25 '15 at 04:28

1 Answers1

2

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.

Roland
  • 18,114
  • 12
  • 62
  • 93
  • Thank you, it worked! Can you help me further, data won't add to the table view... Full code posted above. – johnsnow333 Mar 26 '15 at 02:11
  • When I see your Person class, I suggest you check out this tutorial: http://code.makery.ch/library/javafx-8-tutorial/ and learn from it. – Roland Mar 26 '15 at 03:00
  • The person class is just a fake frame. I will implement real database once all works. maker.ch has no database implementation. I tried to modify it, but in vain. Please, I really need help – johnsnow333 Mar 27 '15 at 00:16
  • You still don't provide all the information. Like eg what's the database look like? I suggest you change your query to "Select CLIENTID, FIRSTNAME, LASTNAME from INFO" and then instead of assigning the data to a property just output them to the console. Or use a debugger. Still, you should process the tutorial, the code in your Person class is just bad with e. g. the public accessor to the property. – Roland Mar 27 '15 at 07:03