Java 将数据库中的数据显示到tableView中

Java 将数据库中的数据显示到tableView中,java,javafx,tableview,javafx-8,Java,Javafx,Tableview,Javafx 8,我试图将数据库中的一些数据显示到TableView(JavaFX)中 更新: 这是我的控制器类: public class CustomersController implements Initializable { @FXML private static TableView<Customers> tableView; @FXML private static TableColumn customerIDCol; @FXML pr

我试图将数据库中的一些数据显示到TableView(JavaFX)中

更新: 这是我的控制器类:

public class CustomersController implements Initializable   {
    @FXML
    private static TableView<Customers> tableView;
    @FXML
    private static TableColumn customerIDCol;
    @FXML
    private TableColumn<Customers, String> firstName;
    @FXML
    private TableColumn<Customers, String> lastName;
    @FXML
    private TableColumn<Customers, String> address;
    @FXML
    private Button addButton;
    @FXML
    private Button addRemove;
    @FXML
    private Button addUpdate;
    @FXML
    private TextField searchBar;
    @FXML
    private Button goButton;
    @FXML
    private Button bookButton;
    @FXML
    private ObservableList<Customers> data;
    /**
     * Initializes the controller class.
     */
    private static Connection con;
    private static Statement stat;
    private PreparedStatement prep;
    Database db = Database.getInstance();
    ResultSet rs = null;    

    public void initialize(URL url, ResourceBundle rb) {
        data = FXCollections.observableArrayList();
        try{
           String sql = "SELECT * FROM FRUITS ";
           rs = db.query(sql);
           con = DriverManager.getConnection("jdbc:sqlite:src/customers/fruits.db");
           stat = con.createStatement();
           while(rs.next()){
               Customers cn = new Customers();
               cn.firstName.set(rs.getString("FirstName"));
               data.add(cn);

               System.out.println(rs.getString("FirstName"));

           }
           firstName.setCellValueFactory(new PropertyValueFactory("firstName"));

           tableView.setItems(data);
           tableView.getColumns().add(firstName); 
       }
       catch (Exception e){
           e.printStackTrace();;
           System.out.println("error");
       }    
    }    

    @FXML
    private void handleAddButton(ActionEvent event) throws IOException{

    }

}
控制台中的错误是:

java.lang.NullPointerException
at customers.CustomersController.initialize(CustomersController.java:98)
第98行是tableView.setItems(数据)

Customers.java

public class Customers{
    public  SimpleIntegerProperty customerID = new SimpleIntegerProperty();
    public  SimpleStringProperty firstName = new SimpleStringProperty();
    public  SimpleStringProperty lastName = new SimpleStringProperty();
    public  SimpleStringProperty address = new SimpleStringProperty();
    public  SimpleStringProperty customerType = new SimpleStringProperty();
    public  SimpleStringProperty phone = new SimpleStringProperty();
    public  SimpleStringProperty email = new SimpleStringProperty();

    public int getCustomerID() {
        return this.customerID.get();
     }
    public void setCustomerID(int id) {
          this.customerID.set(id);
     }

    public String getFirstName() {
        return firstName.get();
     }
    public void setFirstName(String firstNameStr) {
          firstName.set(firstNameStr);
     }
    public String getLastName() {
        return lastName.get();
     }
    public void setLastName(String lastNameStr) {
          lastName.set(lastNameStr);
     }
    public String getAddress() {
        return address.get();
     }
    public void setAddress(String address) {
          this.address.set(address);
     }
    public void setCustomerType(String type) {
          this.customerType.set(type);
     }

    public String getCustomerType() {
        return this.customerType.get();
     }
    public void setPhone(String phone) {
          this.phone.set(phone);
     }
    public String getPhone() {
        return this.phone.get();
     }
    public void setEmail(String email) {
         this.email.set(email);
     }
    public String getEmail() {
        return this.email.get();
     }

}
康托勒班

public class CustomersController implements Initializable   {
    @FXML
    private static TableView<Customers> tableView;
    @FXML
    private static TableColumn<Customers, int> customerIDCol;
    @FXML
    private TableColumn<Customers, String> firstName;
    @FXML
    private TableColumn<Customers, String> lastName;
    @FXML
    private TableColumn<Customers, String> CustomerType;
    /**
     * Initializes the controller class.
     */
    private ObservableList<Customers> data;
    private static Connection con;
    private static Statement stat;
    private PreparedStatement prep;
    Database db = Database.getInstance();
    ResultSet rs = null;


    public void initialize(URL url, ResourceBundle rb) {
        data = FXCollections.observableArrayList();
       try{

           while(rs.next()){
               data.add(new Customers(
                    rs.getInt("CustomerID"),
                    rs.getString("FirstName"),
                    rs.getString("LastName"),
                    rs.getString("CustomerType"),
                    rs.getString("Address"),
                    rs.getString("Phone"),
                    rs.getString("Email")
               ));
           }
           CustomerIDCol.setCellValueFactory(new PropertyValueFactory("customerID"));
           firstName.setCellValueFactory(new PropertyValueFactory("firstName"));
           lastName.setCellValueFactory(new PropertyValueFactory("lastName"));
           CustomerType.setCellValueFactory(new PropertyValueFactory("customerTpe"));
           address.setCellValueFactory(new PropertyValueFactory("address"));
           phone.setCellValueFactory(new PropertyValueFactory("phone"));
           email.setCellValueFactory(new PropertyValueFactory("email"));

           tableView.setItems(data);             

       }catch (Exception e){
           e.printStackTrace();;
           System.out.println("error");
       }

    }    

    @FXML
    private void handleAddButton(ActionEvent event) throws IOException{


        }

    }

}
公共类CustomerController实现可初始化{
@FXML
私有静态TableView TableView;
@FXML
私有静态TableColumn customerIDCol;
@FXML
私有表列名;
@FXML
私有表列lastName;
@FXML
私有表列CustomerType;
/**
*初始化控制器类。
*/
私有可观测列表数据;
专用静态连接;
私有静态语句stat;
私人编制报表;
Database db=Database.getInstance();
结果集rs=null;
公共void初始化(URL、ResourceBundle rb){
data=FXCollections.observearraylist();
试一试{
while(rs.next()){
数据。添加(新客户)(
rs.getInt(“客户ID”),
rs.getString(“名字”),
rs.getString(“LastName”),
rs.getString(“CustomerType”),
rs.getString(“地址”),
rs.getString(“电话”),
rs.getString(“电子邮件”)
));
}
CustomerIDCol.setCellValueFactory(新属性ValueFactory(“customerID”));
firstName.setCellValueFactory(新属性ValueFactory(“firstName”);
setCellValueFactory(新属性ValueFactory(“lastName”);
CustomerType.setCellValueFactory(新属性ValueFactory(“CustomerType”);
地址:setCellValueFactory(新PropertyValueFactory(“地址”);
setCellValueFactory(新属性ValueFactory(“电话”));
email.setCellValueFactory(新PropertyValueFactory(“电子邮件”);
tableView.setItems(数据);
}捕获(例外e){
e、 printStackTrace();;
System.out.println(“错误”);
}
}    
@FXML
私有void handleAddButton(ActionEvent事件)引发IOException{
}
}
}
FXML代码

 <children>
            <TableView id="tableView" fx:id="tableView" prefHeight="272.0" prefWidth="887.0">
              <columns>
                <TableColumn id="CustomerIDCol" fx:id="customerIDCol" prefWidth="130.0" text="CustomerID" />
                <TableColumn id="firstName" fx:id="firstName" prefWidth="130.0" text="FirstName" />
                  <TableColumn id="lastName" fx:id="lastName" prefWidth="130.0" text="LastName" />
                  <TableColumn id="CustomerType" fx:id="CustomerType" prefWidth="130.0" text="CustomerType" />
                  <TableColumn id="address" fx:id="address" prefWidth="130.0" text="Address" />
                  <TableColumn fx:id="phone" prefWidth="130.0" text="Phone" />
                  <TableColumn fx:id="email" prefWidth="110.0" text="Email" />
              </columns>
            </TableView>
         </children>


首先删除
data=FXCollections.observearraylist()来自cyclenothing changed,同样的错误。您是否能够在
表之前打印出
数据的值。setItems(data)
?请不要破坏您的问题。这是一个问答网站,而不是帮助网站,在这种情况下,问题的质量至关重要。如果你诋毁了这个问题,以至于答案没有那么有意义,你就破坏了这个问题对未来访问者的价值。诋毁自己的问题与诋毁别人的答案一样糟糕,诋毁别人的答案更糟糕。不要那样做。评论不是为了进行长时间的讨论;这段对话已经结束。
public class CustomersController implements Initializable   {
    @FXML
    private static TableView<Customers> tableView;
    @FXML
    private static TableColumn<Customers, int> customerIDCol;
    @FXML
    private TableColumn<Customers, String> firstName;
    @FXML
    private TableColumn<Customers, String> lastName;
    @FXML
    private TableColumn<Customers, String> CustomerType;
    /**
     * Initializes the controller class.
     */
    private ObservableList<Customers> data;
    private static Connection con;
    private static Statement stat;
    private PreparedStatement prep;
    Database db = Database.getInstance();
    ResultSet rs = null;


    public void initialize(URL url, ResourceBundle rb) {
        data = FXCollections.observableArrayList();
       try{

           while(rs.next()){
               data.add(new Customers(
                    rs.getInt("CustomerID"),
                    rs.getString("FirstName"),
                    rs.getString("LastName"),
                    rs.getString("CustomerType"),
                    rs.getString("Address"),
                    rs.getString("Phone"),
                    rs.getString("Email")
               ));
           }
           CustomerIDCol.setCellValueFactory(new PropertyValueFactory("customerID"));
           firstName.setCellValueFactory(new PropertyValueFactory("firstName"));
           lastName.setCellValueFactory(new PropertyValueFactory("lastName"));
           CustomerType.setCellValueFactory(new PropertyValueFactory("customerTpe"));
           address.setCellValueFactory(new PropertyValueFactory("address"));
           phone.setCellValueFactory(new PropertyValueFactory("phone"));
           email.setCellValueFactory(new PropertyValueFactory("email"));

           tableView.setItems(data);             

       }catch (Exception e){
           e.printStackTrace();;
           System.out.println("error");
       }

    }    

    @FXML
    private void handleAddButton(ActionEvent event) throws IOException{


        }

    }

}
 <children>
            <TableView id="tableView" fx:id="tableView" prefHeight="272.0" prefWidth="887.0">
              <columns>
                <TableColumn id="CustomerIDCol" fx:id="customerIDCol" prefWidth="130.0" text="CustomerID" />
                <TableColumn id="firstName" fx:id="firstName" prefWidth="130.0" text="FirstName" />
                  <TableColumn id="lastName" fx:id="lastName" prefWidth="130.0" text="LastName" />
                  <TableColumn id="CustomerType" fx:id="CustomerType" prefWidth="130.0" text="CustomerType" />
                  <TableColumn id="address" fx:id="address" prefWidth="130.0" text="Address" />
                  <TableColumn fx:id="phone" prefWidth="130.0" text="Phone" />
                  <TableColumn fx:id="email" prefWidth="110.0" text="Email" />
              </columns>
            </TableView>
         </children>