所以我有这段代码:
public class UIManager {
private MainMenuService serviceBoi;
public UIManager(){
serviceBoi = new MainMenuService();
}
@FXML
private ResourceBundle resources;
@FXML
private URL location;
@FXML
private TableColumn<?, ?> reservationBookingStatusColumn;
@FXML
private Button reservationDeleteButton;
@FXML
private Button reservationEditButton;
@FXML
private TableColumn<?, ?> reservationIbanColumn;
@FXML
private Button reservationNewButton;
@FXML
private TableColumn<?, ?> reservationRStartDateColumn;
@FXML
private TableColumn<?, ?> reservationRentalEndColumn;
@FXML
private Button reservationSearchButton;
@FXML
private TableColumn<?, ?> reservationsCustomerNameColumn;
@FXML
private Tab reservationsTab;
@FXML
private TableColumn<?, ?> reservationsTotalPriceColumn;
@FXML
private TableColumn<VehicleDTO, Short> vehicleCapacityColumn;
@FXML
private TableColumn<VehicleDTO, Integer> vehicleConstructionDateColumn;
@FXML
private Button vehicleDeleteButton;
@FXML
private Button vehicleEditButton;
@FXML
private TableColumn<VehicleDTO, String> vehicleModelNameColumn;
@FXML
private TableColumn<VehicleDTO, Boolean> vehicleMotorisedColumn;
@FXML
private Button vehicleNewButton;
@FXML
private TableColumn<VehicleDTO, String> vehiclePlateNumberColumn;
@FXML
private TableColumn<VehicleDTO, Integer> vehiclePowerColumn;
@FXML
private TableColumn<VehicleDTO, Double> vehiclePriceColumn;
@FXML
private Button vehicleSearchButton;
@FXML
private TableView<VehicleDTO> vehicleTable;
@FXML
private Tab vehiclesTab;
@FXML
void initialize() {
assert reservationBookingStatusColumn != null : "fx:id=\"reservationBookingStatusColumn\" was not injected: check your FXML file 'mainMenu.fxml'.";
assert reservationDeleteButton != null : "fx:id=\"reservationDeleteButton\" was not injected: check your FXML file 'mainMenu.fxml'.";
assert reservationEditButton != null : "fx:id=\"reservationEditButton\" was not injected: check your FXML file 'mainMenu.fxml'.";
assert reservationIbanColumn != null : "fx:id=\"reservationIbanColumn\" was not injected: check your FXML file 'mainMenu.fxml'.";
assert reservationNewButton != null : "fx:id=\"reservationNewButton\" was not injected: check your FXML file 'mainMenu.fxml'.";
assert reservationRStartDateColumn != null : "fx:id=\"reservationRStartDateColumn\" was not injected: check your FXML file 'mainMenu.fxml'.";
assert reservationRentalEndColumn != null : "fx:id=\"reservationRentalEndColumn\" was not injected: check your FXML file 'mainMenu.fxml'.";
assert reservationSearchButton != null : "fx:id=\"reservationSearchButton\" was not injected: check your FXML file 'mainMenu.fxml'.";
assert reservationsCustomerNameColumn != null : "fx:id=\"reservationsCustomerNameColumn\" was not injected: check your FXML file 'mainMenu.fxml'.";
assert reservationsTab != null : "fx:id=\"reservationsTab\" was not injected: check your FXML file 'mainMenu.fxml'.";
assert reservationsTotalPriceColumn != null : "fx:id=\"reservationsTotalPriceColumn\" was not injected: check your FXML file 'mainMenu.fxml'.";
assert vehicleCapacityColumn != null : "fx:id=\"vehicleCapacityColumn\" was not injected: check your FXML file 'mainMenu.fxml'.";
assert vehicleConstructionDateColumn != null : "fx:id=\"vehicleConstructionDateColumn\" was not injected: check your FXML file 'mainMenu.fxml'.";
assert vehicleDeleteButton != null : "fx:id=\"vehicleDeleteButton\" was not injected: check your FXML file 'mainMenu.fxml'.";
assert vehicleEditButton != null : "fx:id=\"vehicleEditButton\" was not injected: check your FXML file 'mainMenu.fxml'.";
assert vehicleModelNameColumn != null : "fx:id=\"vehicleModelNameColumn\" was not injected: check your FXML file 'mainMenu.fxml'.";
assert vehicleMotorisedColumn != null : "fx:id=\"vehicleMotorisedColumn\" was not injected: check your FXML file 'mainMenu.fxml'.";
assert vehicleNewButton != null : "fx:id=\"vehicleNewButton\" was not injected: check your FXML file 'mainMenu.fxml'.";
assert vehiclePlateNumberColumn != null : "fx:id=\"vehiclePlateNumberColumn\" was not injected: check your FXML file 'mainMenu.fxml'.";
assert vehiclePowerColumn != null : "fx:id=\"vehiclePowerColumn\" was not injected: check your FXML file 'mainMenu.fxml'.";
assert vehiclePriceColumn != null : "fx:id=\"vehiclePriceColumn\" was not injected: check your FXML file 'mainMenu.fxml'.";
assert vehicleSearchButton != null : "fx:id=\"vehicleSearchButton\" was not injected: check your FXML file 'mainMenu.fxml'.";
assert vehicleTable != null : "fx:id=\"vehicleTable\" was not injected: check your FXML file 'mainMenu.fxml'.";
assert vehiclesTab != null : "fx:id=\"vehiclesTab\" was not injected: check your FXML file 'mainMenu.fxml'.";
}
@FXML
public void openNewCarWindow(){
try{
FXMLLoader fxload = new FXMLLoader(getClass().getResource("/fxml/newCar.fxml"));
Parent daddy = fxload.load();
Stage stage = new Stage();
stage.setTitle("Register a new VehicleDTO");
stage.setScene(new Scene(daddy));
stage.show();
}catch (Exception e){
e.printStackTrace();
}
}
public void fillTable(){
List<VehicleDTO> vroomys = serviceBoi.fetchVroomList();
vehicleModelNameColumn.setCellValueFactory(new PropertyValueFactory<>("charName")); //this is line 159
vehicleMotorisedColumn.setCellValueFactory(new PropertyValueFactory<>("motor"));
vehicleConstructionDateColumn.setCellValueFactory(new PropertyValueFactory<>("constructionDate"));
vehicleCapacityColumn.setCellValueFactory(new PropertyValueFactory<>("capacity"));
vehiclePlateNumberColumn.setCellValueFactory(new PropertyValueFactory<>("plateNumber"));
vehiclePowerColumn.setCellValueFactory(new PropertyValueFactory<>("powerKW"));
vehiclePriceColumn.setCellValueFactory(new PropertyValueFactory<>("rentPrice"));
vehicleTable.setItems(getObservableVrooms(vroomys));
}
private ObservableList<VehicleDTO> getObservableVrooms(List<VehicleDTO> vehicleList) {
try {
ObservableList<VehicleDTO> observableList = FXCollections.observableArrayList(vehicleList);
return observableList;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
应该使用VehicleDTO列表中的变量填充表格。列表本身已填充(我已检查),VehicleDTO类如下所示:
public class VehicleDTO {
private int vid;
private String charName;
private int constructionDate;
private String description;
private short capacity;
private String plateNumber;
private boolean motor;
private short powerKW;
private double rentPrice;
private java.sql.Date entryDate;
private boolean deleted;
private String picturePath;
private List<Licenses> requiredLicenses;
private java.sql.Timestamp lastEdit;
@Override
public String toString() {
return "VehicleDTO{" +
"vid=" + vid +
", charName='" + charName + '\'' +
", constructionDate=" + constructionDate +
", description='" + description + '\'' +
", capacity=" + capacity +
", plateNumber='" + plateNumber + '\'' +
", motor=" + motor +
", powerKW=" + powerKW +
", rentPrice=" + rentPrice +
", entryDate=" + entryDate +
", deleted=" + deleted +
", picturePath='" + picturePath + '\'' +
", requiredLicenses=" + requiredLicenses +
'}';
}
public VehicleDTO(String charName, boolean motor, double rentPrice, Date entryDate, boolean deleted, List<Licenses> requiredLicenses) {
this.charName = charName;
this.motor = motor;
this.rentPrice = rentPrice;
this.entryDate = entryDate;
this.deleted = deleted;
this.requiredLicenses = requiredLicenses;
}
排除所有getter / setter方法。 UIManager类连接到此FXML文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="532.0" prefWidth="849.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="at.ac.tuwien.sepm.assignment.individual.universe.ui.UIManager">
<children>
<TabPane layoutX="14.0" layoutY="14.0" prefHeight="504.0" prefWidth="821.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab fx:id="reservationsTab" text="Reservations">
<content>
<AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="468.0" prefWidth="821.0">
<children>
<TableView layoutX="0.0" layoutY="0.0" prefHeight="422.0" prefWidth="821.0">
<columns>
<TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="125.0" text="Customer Name" fx:id="reservationsCustomerNameColumn" />
<TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="138.0" text="IBAN/Credit Card" fx:id="reservationIbanColumn" />
<TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="97.0" text=" Status" fx:id="reservationBookingStatusColumn" />
<TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="138.0" text="Rental Start Date" fx:id="reservationRStartDateColumn" />
<TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="124.0" text="Rental End Date" fx:id="reservationRentalEndColumn" />
<TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="109.0" text="Total Price" fx:id="reservationsTotalPriceColumn" />
</columns>
</TableView>
<Button id="reservationEdit" fx:id="reservationEditButton" layoutX="0.0" layoutY="434.0" mnemonicParsing="false" text="Edit" />
<Button fx:id="reservationNewButton" layoutX="72.0" layoutY="434.0" mnemonicParsing="false" onAction="#openNewCarWindow" text="New" />
<Button fx:id="reservationSearchButton" layoutX="754.0" layoutY="434.0" mnemonicParsing="false" text="Search" />
<Button fx:id="reservationDeleteButton" layoutX="149.0" layoutY="434.0" mnemonicParsing="false" text="Delete" />
</children>
</AnchorPane>
</content>
</Tab>
<Tab fx:id="vehiclesTab" text="Vehicles">
<content>
<AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TableView fx:id="vehicleTable" layoutX="0.0" layoutY="0.0" prefHeight="419.0" prefWidth="821.0">
<columns>
<TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="104.0" text="Model Name" fx:id="vehicleModelNameColumn" />
<TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="136.0" text="Construction Date" fx:id="vehicleConstructionDateColumn" />
<TableColumn prefWidth="75.0" text="Capacity" fx:id="vehicleCapacityColumn" />
<TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="118.0" text="Plate Number" fx:id="vehiclePlateNumberColumn" />
<TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="125.0" text="Per Hour Price" fx:id="vehiclePriceColumn" />
<TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="105.0" text="Power(KW)" fx:id="vehiclePowerColumn" />
<TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="104.0" text="Motorised" fx:id="vehicleMotorisedColumn" />
</columns>
</TableView>
<Button fx:id="vehicleEditButton" layoutX="0.0" layoutY="434.0" mnemonicParsing="false" text="Edit" />
<Button fx:id="vehicleNewButton" layoutX="72.0" layoutY="434.0" mnemonicParsing="false" text="New" />
<Button fx:id="vehicleDeleteButton" layoutX="148.0" layoutY="434.0" mnemonicParsing="false" text="Delete" />
<Button fx:id="vehicleSearchButton" layoutX="755.0" layoutY="434.0" mnemonicParsing="false" text="Search" />
</children>
</AnchorPane>
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>
这是我得到的错误:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:945)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(Lau ncherImpl.java:973)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.NullPointerException
at at.ac.tuwien.sepm.assignment.individual.universe.ui.UIManager.fillTable(UIManager.java:159)
at at.ac.tuwien.sepm.assignment.individual.application.MainApplication.start(MainApplication.java:49)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
... 1 more
Exception running application at.ac.tuwien.sepm.assignment.individual.application.MainApplication
我已经检查过列表中是否填充了适当的值,并且我尝试使用回调来填充列表。第159行是此错误消息中的行
vehicleModelNameColumn.setCellValueFactory(new PropertyValueFactory<>("charName"));
在fillTable()方法中。 我绝对卡住了请帮忙。