本教程的问题:在JavaFX中使用数据库填充tableview

时间:2015-03-24 04:05:50

标签: java javafx tableview

在学习本教程时,我得到一个nullpointerxception: Populate a tableview using database in JavaFX

我对其进行了修改,使其更简单,更符合我的需求: 而不是Usermaster,我有Person对象。

 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));

由于nullpointerxception,代码在per.ClientID.set(rs.getInt(1));处停止。

如果我制作system.out.println(rs.getInt(1))(或任何其他列),我会得到价值......但似乎我无法将其传递给我的对象。

所有Person对象变量都是SimpleString / IntergerProperty类型,如教程中所示。

有人可以帮我识别我在编码时犯的错误吗?

谢谢

**答案:需要初始化值。

现在我没有错误,但我的桌子没有填充...

完整代码:

a)主要应用程序

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();    
}

}

模特课程:

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;    
    }
}

控制器类:

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");            
    }
   }
}

1 个答案:

答案 0 :(得分:2)

ClientID为空。你没有初始化它。

如果它是属性,则应为其创建正确的getter和setter,而不是直接使用该属性。此外,你不应该在ResultSet的getter中使用1,2等。使用列名称是一种更好的做法。