我想将值添加到Javafx表中。
值存储在远程数据库中。
从db(setAll(service.listReservierung()
)中获取值的方法按预期工作。当我尝试运行以下代码以便将值放在Javafx表中时,我得到一个完整的错误列表,第一个是Null指针异常。
这是代码:
public void listReserv() {
try {
AlleReservTable.getItems().setAll(service.listReservierung());
tableColumnReservNr.setCellValueFactory(
new PropertyValueFactory<Reservierung, Integer>("reservNr"));
tableColumnReservName.setCellValueFactory(
new PropertyValueFactory<Reservierung, String>("kundeName"));
tableColumnReservVon.setCellValueFactory(
new PropertyValueFactory<Reservierung, Timestamp>("von"));
tableColumnReservBis.setCellValueFactory(
new PropertyValueFactory<Reservierung, Timestamp>("bis"));
} catch(Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:1)
Hej,我为你做了一个工作示例,我使用了Java8的新Date Time API。 ReservationService不查询数据库,为了检查这种情况,您必须发布您的Service类。也许这个例子会对你有帮助。
FXML文件
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane fx:controller="de.professional_webworkx.reservationmanager.controller.MainController" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="768.0" prefWidth="1024.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children><TableView fx:id="reservationTable" prefHeight="768.0" prefWidth="1024.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" xmlns:fx="http://javafx.com/fxml">
<columns>
<TableColumn fx:id="resNumber" minWidth="250.0" prefWidth="250.0" text="ReservationID" />
<TableColumn fx:id="customerName" minWidth="450.0" prefWidth="500.0" text="CustomerName" />
<TableColumn fx:id="checkIn" minWidth="100.0" prefWidth="100.0" text="CheckIn" />
<TableColumn fx:id="checkOut" minWidth="100.0" prefWidth="100.0" text="CheckOut" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" />
</columns>
</TableView>
</children></AnchorPane>
MainController
package de.professional_webworkx.reservationmanager.controller;
import de.professional_webworkx.reservationmanager.business.ReservationService;
import de.professional_webworkx.reservationmanager.model.Reservation;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.List;
import java.util.ResourceBundle;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.StringProperty;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
/**
* Main FXML Controller
* @author Patrick Ott <Patrick.Ott@professional-webworkx.de>
* @version 1.0
*/
public class MainController implements Initializable {
@FXML
TableView<Reservation> reservationTable;
@FXML
TableColumn<Reservation, Integer> resNumber;
@FXML
TableColumn<Reservation, String> customerName;
@FXML
TableColumn<Reservation, ObjectProperty<LocalDateTime>> checkIn;
@FXML
TableColumn<Reservation, ObjectProperty<LocalDateTime>> checkOut;
@Override
public void initialize(URL url, ResourceBundle rb) {
ReservationService service = new ReservationService();
List<Reservation> allReservations = service.getAllReservations();
reservationTable.getItems().addAll(allReservations);
resNumber.setCellValueFactory(new PropertyValueFactory<>("reserveNumber"));
customerName.setCellValueFactory(new PropertyValueFactory<>("customerName"));
checkIn.setCellValueFactory(new PropertyValueFactory<>("checkIn"));
checkOut.setCellValueFactory(new PropertyValueFactory<>("checkOut"));
}
}
预订实体
package de.professional_webworkx.reservationmanager.model;
import java.time.LocalDate;
import java.util.Random;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
/**
* Reservation entity
* @author Patrick Ott <Patrick.Ott@professional-webworkx.de>
* @version 1.0
*/
public class Reservation {
private IntegerProperty reserveNumberProperty;
private StringProperty customerNameProperty;
private ObjectProperty<LocalDate> checkIn;
private ObjectProperty<LocalDate> checkOut;
public Reservation() {
reserveNumberProperty = new SimpleIntegerProperty(generateReservationNumber());
customerNameProperty = new SimpleStringProperty();
checkIn = new SimpleObjectProperty<>();
checkOut = new SimpleObjectProperty<>();
}
public Reservation(final String customerName, final LocalDate checkIn, final LocalDate checkOut) {
reserveNumberProperty = new SimpleIntegerProperty(generateReservationNumber());
customerNameProperty = new SimpleStringProperty(customerName);
this.checkIn = new SimpleObjectProperty<>(checkIn);
this.checkOut = new SimpleObjectProperty<>(checkOut);
}
/**
* @return the reserveNumberProperty
*/
public IntegerProperty getReserveNumberProperty() {
return reserveNumberProperty;
}
public Integer getReserveNumber() {
return reserveNumberProperty.get();
}
/**
* @param reserveNumberProperty the reserveNumberProperty to set
*/
public void setReserveNumberProperty(IntegerProperty reserveNumberProperty) {
this.reserveNumberProperty = reserveNumberProperty;
}
/**
* @return the customerNameProperty
*/
public StringProperty getCustomerNameProperty() {
return customerNameProperty;
}
public String getCustomerName() {
return customerNameProperty.get();
}
/**
* @param customerNameProperty the customerNameProperty to set
*/
public void setCustomerNameProperty(StringProperty customerNameProperty) {
this.customerNameProperty = customerNameProperty;
}
/**
* @return the checkIn
*/
public ObjectProperty<LocalDate> getCheckInProperty() {
return checkIn;
}
public LocalDate getCheckIn() {
return checkIn.getValue();
}
/**
* @param fromProperty the checkIn to set
*/
public void setCheckInProperty(ObjectProperty<LocalDate> fromProperty) {
this.checkIn = fromProperty;
}
/**
* @return the checkOut
*/
public ObjectProperty<LocalDate> getCheckOutProperty() {
return checkOut;
}
public LocalDate getCheckOut() {
return checkOut.getValue();
}
/**
* @param toProperty the checkOut to set
*/
public void setCheckOutProperty(ObjectProperty<LocalDate> toProperty) {
this.checkOut = toProperty;
}
private Integer generateReservationNumber() {
Random random = new Random();
int nextInt = random.nextInt();
return new Integer(nextInt);
}
}
ReservationService
package de.professional_webworkx.reservationmanager.business;
import de.professional_webworkx.reservationmanager.model.Reservation;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
/**
* ReservationService
* persist, query, delete, edit Reservations
* @author Patrick Ott <Patrick.Ott@professional-webworkx.de>
* @version 1.0
*/
public class ReservationService {
public ReservationService() {
super();
}
public List<Reservation> getAllReservations() {
List<Reservation> reservations = new ArrayList<>();
// fetch all reservations from database
reservations.add(new Reservation("Patrick", LocalDate.now(), LocalDate.now().plusWeeks(3)));
reservations.add(new Reservation("userXYZ", LocalDate.now(), LocalDate.now().plusWeeks(2)));
return reservations;
}
}
然后开始
package de.professional_webworkx.reservationmanager;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author Patrick Ott <Patrick.Ott@professional-webworkx.de>
*/
public class ReservationManager extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
Parent parent = FXMLLoader.load(getClass().getResource("main.fxml"));
Scene scene = new Scene(parent);
primaryStage.setTitle("Hotel Reservation Manager v1.0");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}