我有在JavaFX TreeView中显示数据库结果的应用程序。我完全按照this Answer中提到的那样做了。
但是当我运行该程序时,我得到如下错误。
错误指向控制器中的tblView.setItems(data)
方法。
我怎么能解决这个问题??
java.lang.NullPointerException
at demofx.FXMLDocumentController.buildData(FXMLDocumentController.java:105)
at demofx.FXMLDocumentController.initialize(FXMLDocumentController.java:85)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at demofx.DemoFX.start(DemoFX.java:23)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
这是我的.fxml文档
<AnchorPane id="AnchorPane" prefHeight="537.0" prefWidth="736.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="demofx.FXMLDocumentController">
<children>
<Label fx:id="lblAuto_id" layoutX="53.0" layoutY="31.0" text="Label" textFill="#da1b1b">
<font>
<Font size="19.0" />
</font>
</Label>
<Label fx:id="lblVtype" layoutX="229.0" layoutY="89.0" text="Selected type" />
<TableView fx:id="tblVehicle" layoutX="38.0" layoutY="142.0" prefHeight="200.0" prefWidth="660.0">
<columns>
<TableColumn fx:id="c_reg" prefWidth="71.0" text="Reg no" />
<TableColumn fx:id="c_make" prefWidth="79.0" text="Make" />
<TableColumn fx:id="c_model" prefWidth="75.0" text="Model" />
<TableColumn fx:id="c_year" prefWidth="75.0" text="year" />
<TableColumn fx:id="c_color" prefWidth="75.0" text="color" />
<TableColumn fx:id="c_odo" prefWidth="75.0" text="odo" />
<TableColumn fx:id="c_status" prefWidth="75.0" text="status" />
<TableColumn fx:id="c_fuel" prefWidth="75.0" text="fuel" />
<TableColumn fx:id="c_type" prefWidth="75.0" text="type" />
</columns>
</TableView>
<ComboBox fx:id="cmbVehicleType" layoutX="30.0" layoutY="85.0" onAction="#handleButtonAction" prefWidth="150.0" />
</children>
</AnchorPane>
这是控制器
public class FXMLDocumentController implements Initializable {
@FXML
private Label lblAuto_id;
@FXML
private ComboBox cmbVehicleType;
@FXML
private Label lblVtype;
@FXML private TableView<Veh> tblView;
@FXML private TableColumn <Veh,String> c_reg;
@FXML private TableColumn <Veh,String> c_make;
@FXML private TableColumn <Veh,String> c_model;
@FXML private TableColumn <Veh,String> c_year;
@FXML private TableColumn <Veh,String> c_color;
@FXML private TableColumn <Veh,String> c_odo;
@FXML private TableColumn <Veh,String> c_status;
@FXML private TableColumn <Veh,String> c_fuel;
@FXML private TableColumn <Veh,String> c_type;
private ObservableList<Veh> data;
DBConnection db = new DBConnection();
@FXML
private void handleButtonAction(ActionEvent event) {
}
@Override
public void initialize(URL url, ResourceBundle rb) {
c_reg.setCellValueFactory(new PropertyValueFactory<Veh, String>("regno"));
c_make.setCellValueFactory(new PropertyValueFactory<Veh, String>("make"));
c_model.setCellValueFactory(new PropertyValueFactory<Veh, String>("model"));
c_year.setCellValueFactory(new PropertyValueFactory<Veh, String>("type"));
c_color.setCellValueFactory(new PropertyValueFactory<Veh, String>("status"));
c_odo.setCellValueFactory(new PropertyValueFactory<Veh, String>("color"));
buildData();
}
public void buildData() {
try {
Connection con = db.get_connection();
data = FXCollections.observableArrayList();
PreparedStatement stat = con.prepareStatement("SELECT * FROM Vehicle_tbl WHERE Status= 'IN' and V_type='CAR'");
ResultSet rs = stat.executeQuery();
while (rs.next()) {
Veh ve = new Veh();
ve.regno.set(rs.getString(1));
ve.make.set(rs.getString(2));
ve.model.set(rs.getString(3));
ve.color.set(rs.getString(4));
ve.status.set(rs.getString(5));
ve.type.set(rs.getString(6));
data.add(ve);
}
tblView.setItems(data);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
这是POJO类
public class Veh {
public SimpleStringProperty regno = new SimpleStringProperty();
public SimpleStringProperty make = new SimpleStringProperty();
public SimpleStringProperty model = new SimpleStringProperty();
public SimpleStringProperty type = new SimpleStringProperty();
public SimpleStringProperty status = new SimpleStringProperty();
public SimpleStringProperty color = new SimpleStringProperty();
public String getRegno(){
return regno.get();
}
public String getMake(){
return make.get();
}
public String getModel(){
return model.get();
}
public String getType(){
return type.get();
}
public String getStatus(){
return status.get();
}
public String getColor(){
return color.get();
}
}
答案 0 :(得分:3)
问题已解决,在控制器中我声明了TableView tblView
,但在.fxml
中它是tblVehicle
。两个名称应该相同,所以我将控制器更改为tblVehicle
。现在它正在运作