我开始学习javaFX,我需要使用数据库中的数据填充表格。我试图理解不同的做法但不断出错
(由:java.lang.NullPointerException
引起at common.logic.RepairContoller.filltable(RepairController.java:81)
common.logic.RepairContoller.initializable(ReepairController.java:54))。
这是我可以帮助改变或提供任何建议的地方:
我的控制器类
package common.logic;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.ResourceBundle;
public class RepairController implements Initializable {
private ArrayList<Booking> bookings = new ArrayList<>();
@FXML public TableView <Booking> bookingTable;
@FXML public TableColumn<Booking, Integer> id;
@FXML public TableColumn<Booking,String> vReg;
@FXML public TableColumn<Booking, String> mechanic;
@FXML public TableColumn<Booking, Integer> duruation;
@FXML public TableColumn<Booking, String> bType;
@FXML public TableColumn<Booking, String> Date;
@FXML public TableColumn<Booking, String> SPC;
@FXML public TableColumn<Booking, Integer> Part;
@FXML public TableColumn<Booking, String> complete;
@FXML public Button editbooking1;
@FXML public Button addbooking1;
@FXML public Button deletebooking1;
@FXML private TextField search_bar;
private databconenction dbc = databconenction.getInstance();
private static Booking selected;
public void initialize(URL location, ResourceBundle resourceBundle)
{
fillTable();
search_bar.textProperty().addListener((ob, oldVal, newVal) -> Search());
String sql = "SELECT * FROM Booking";
ResultSet rsb = dbc.query(sql);
try {
while (rsb.next())
{
bookings.add(new Booking(rsb));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void Search() {
}
public void fillTable()
{
setValueFactories();
ArrayList<Booking> tableValues = new ArrayList<>();
String sql = "SELECT * FROM Booking";
ResultSet rsb = dbc.query(sql);
try {
while (true)
{
if (!(rsb.next())) break;
tableValues.add(new Booking(rsb));
}
bookingTable.setItems(FXCollections.observableArrayList(tableValues));
} catch (SQLException e) {
e.printStackTrace();
}
}
private void setValueFactories() {
id.setCellValueFactory(new PropertyValueFactory<>("id"));
vReg.setCellValueFactory(new PropertyValueFactory<>("vReg"));
mechanic.setCellValueFactory(new PropertyValueFactory<>("mechanic"));
duruation.setCellValueFactory(new PropertyValueFactory<>("duruation"));
bType.setCellValueFactory(new PropertyValueFactory<>("bTtype"));
Date.setCellValueFactory(new PropertyValueFactory<>("Date"));
SPC.setCellValueFactory(new PropertyValueFactory<>("SPC"));
Part.setCellValueFactory(new PropertyValueFactory<>("Part"));
complete.setCellValueFactory(new PropertyValueFactory<>("complete"));
}
@FXML
public void addB() {
System.out.println("You want to add a booking");
try {
Stage stage = new Stage();
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample/add.fxml"));
Parent root1 = fxmlLoader.load();
stage.setScene(new Scene(root1));
stage.show();
}
catch (IOException e)
{
e.printStackTrace();
}
}
@FXML
public void editB(ActionEvent f) {
System.out.println("You want to edit a booking");
try {
Stage stage = new Stage();
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample/edit.fxml"));
Parent root2 = fxmlLoader.load();
stage.setScene(new Scene(root2));
stage.show();
}
catch (IOException a)
{
a.printStackTrace();
}
}
@FXML
public void deleteB(ActionEvent d) {
System.out.println("You want to delete a booking");
try {
Stage stage = new Stage();
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample/delete.fxml"));
Parent root3 = fxmlLoader.load();
stage.setScene(new Scene(root3));
stage.show();
}
catch (IOException ie)
{
ie.printStackTrace();
}
}
}
数据库连接类
package common.logic;
import java.sql.DriverManager ;
import java.sql.*;
import java.sql.Connection;
import java.sql.ResultSet ;
public class databconenction {
private static final databconenction INSTANCE = new databconenction();
public Connection con = null;
public databconenction() { connect();}
public static databconenction getInstance() { return INSTANCE;}
public Connection connect()
{
if(con !=null)
return con;
try
{
Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("JDBC:sqlite:C:\\Users\\Don\\Documents\\SE25-test\\scratch\\Donovan\\sample\\GM_SIS_02.db");
} catch (ClassNotFoundException e)
{
System.out.println("Driver not found!");
e.getException();
} catch (SQLException se){
System.out.println("NO CONNECTION");
se.getMessage();
}
return con;
}
public ResultSet query(String sql)
{
ResultSet rs = null;
try
{
Statement stmt = con
.createStatement();
rs = stmt.executeQuery(sql);
}
catch (SQLException se)
{
se.printStackTrace();
}
return rs;
}
public void update(String sql){
try{
Statement stmt = con.createStatement();
stmt.executeUpdate(sql);
} catch (SQLException se){
se.printStackTrace();
} catch (NullPointerException ignored){
}
}
}
GUI FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<Pane minHeight="0.0" minWidth="0.0" prefHeight="507.0" prefWidth="897.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="common.logic.RepairController">
<children>
<TableView fx:id="customerTable" layoutX="12.0" layoutY="170.0" prefHeight="267.0" prefWidth="877.0">
<columns>
<TableColumn fx:id="id" prefWidth="97.4" text="Booking ID" />
<TableColumn fx:id="vReg" prefWidth="97.4" text="Vehicle Reg" />
<TableColumn fx:id="mechanic" prefWidth="97.4" text="Mechanic" />
<TableColumn fx:id="duruation" prefWidth="97.4" text="Duruation" />
<TableColumn fx:id="bType" prefWidth="97.4" text="Booking Type" />
<TableColumn fx:id="Date" prefWidth="97.4" text="Date" />
<TableColumn fx:id="SPC" prefWidth="97.4" text="SPC" />
<TableColumn fx:id="Part" prefWidth="97.4" text="Part" />
<TableColumn fx:id="complete" prefWidth="97.4" text="Complete" />
</columns>
</TableView>
<Text layoutX="22.0" layoutY="95.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Diagnosis and Repairs / Scheduled Maintenance">
<font>
<Font name="Cambria" size="36.0" />
</font>
</Text>
<Button layoutX="757.0" layoutY="109.0" mnemonicParsing="false" text="Enter" />
<Button fx:id="addbooking1" layoutX="22.0" layoutY="454.0" mnemonicParsing="false" onAction="#addB" text="Add" />
<Button fx:id="editbooking1" layoutX="126.0" layoutY="454.0" mnemonicParsing="false" onAction="#editB" text="Edit" />
<Button fx:id="deletebooking1" layoutX="229.0" layoutY="454.0" mnemonicParsing="false" onAction="#deleteB" text="Delete" />
</children>
</Pane>