在javaFX TableView中格式化从Oracle DB检索的数据的日期列
我按照帖子要求设置日期格式,但得到了java.lang.NullPointerException 控制器类: 这是我的密码: FXML文件:在javaFX TableView中格式化从Oracle DB检索的数据的日期列,java,oracle,Java,Oracle,我按照帖子要求设置日期格式,但得到了java.lang.NullPointerException 控制器类: 这是我的密码: FXML文件: <AnchorPane id="AnchorPane" prefHeight="864.0" prefWidth="1090.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="idehmis.control
<AnchorPane id="AnchorPane" prefHeight="864.0" prefWidth="1090.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="idehmis.controller.PersonController">
<children>
<BorderPane layoutX="418.0" layoutY="159.0" prefHeight="705.0" prefWidth="1090.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="159.0">
<center>
<TableView fx:id="personTable" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<columns>
<TableColumn prefWidth="75.0" text="C1" />
<TableColumn prefWidth="75.0" text="C2" />
</columns>
</TableView>
</center>
</BorderPane>
<JFXTextField fx:id="Pmrn" layoutX="49.0" layoutY="43.0" maxWidth="117.0" minWidth="106.0" nodeOrientation="LEFT_TO_RIGHT" prefColumnCount="8" prefHeight="31.0" prefWidth="117.0" promptText="MRN" unFocusColor="#0f098a">
</JFXTextField>
<JFXTextField fx:id="Plastname" layoutX="47.0" layoutY="88.0" maxWidth="121.0" minWidth="106.0" nodeOrientation="LEFT_TO_RIGHT" prefColumnCount="8" prefHeight="31.0" prefWidth="117.0" promptText="Last Name" unFocusColor="#190879">
</JFXTextField>
<JFXDatePicker fx:id="Pdateofbirth" layoutX="210.0" layoutY="43.0" prefHeight="31.0" prefWidth="201.0" promptText="Date Of Birth" />
<Button fx:id="getAllData" layoutX="573.0" layoutY="30.0" mnemonicParsing="false" onAction="#SubmitButton" text="Retrieve data" />
</children>
</AnchorPane>
以下是Person类:
public class Person {
public final StringProperty mrn;
public final StringProperty lastname;
//public final StringProperty dateofbirth;
public ObjectProperty<LocalDate> dateofbirth = new SimpleObjectProperty<>();
public Person() {
this.mrn = new SimpleStringProperty();
this.lastname = new SimpleStringProperty();
//this.dateofbirth = new SimpleStringProperty();
this.dateofbirth = new SimpleObjectProperty<>();
}
public String getMRN() {
return mrn.get();
}
public void setMRN(String mrn) {
this.mrn.set(mrn);
}
public StringProperty mrnProperty() {
return mrn;
}
public String getLarstName() {
return lastname.get();
}
public void setLastName(String lastName) {
this.lastname.set(lastName);
}
public StringProperty lastNameProperty() {
return lastname;
}
public Object getDateOfBirth() {
return dateofbirth.get();
}
public void setDateOfBirth(LocalDate dateofbirth) {
this.dateofbirth.set(dateofbirth);
}
public ObjectProperty<LocalDate> dateOfBirthProperty() {
return dateofbirth;
}
}
公共类人物{
公共财产;
公共属性lastname;
//公众最终出生日期;
public ObjectProperty dateofbirth=新的SimpleObject属性();
公众人士(){
this.mrn=新的SimpleStringProperty();
this.lastname=新的SimpleStringProperty();
//this.dateofbirth=新的SimpleStringProperty();
this.dateofbirth=新的SimpleObject属性();
}
公共字符串getMRN(){
返回mrn.get();
}
公共无效设置mrn(字符串mrn){
此.mrn.set(mrn);
}
公共属性mrnProperty(){
返回mrn;
}
公共字符串getLarstName(){
返回lastname.get();
}
public void setLastName(字符串lastName){
this.lastname.set(lastname);
}
公共StringProperty lastNameProperty(){
返回姓氏;
}
公共对象getDateOfBirth(){
返回birth.get()的日期;
}
公共无效setDateOfBirth(本地出生日期){
this.dateofbirth.set(dateofbirth);
}
公共对象属性dateOfBirthProperty(){
出生返回日期;
}
}
这是控制器:
public class PersonController implements Initializable {
@FXML
private TableView<Person> personTable;
@FXML
private JFXTextField Pmrn;
@FXML
private JFXTextField Plastname;
@FXML
private JFXDatePicker Pdateofbirth;
@FXML
private ObservableList<Person> data;
@FXML
private Button getAllData;
@FXML
TableColumn<Person, String> MRN;
@FXML
TableColumn<Person, String> LASTNAME;
@FXML
TableColumn<Person, LocalDate> DATEOFBIRTH;
SimpleDateFormat sdf = new SimpleDateFormat("dd,MMM,yyyy", Locale.getDefault());
@Override
public void initialize(URL url, ResourceBundle rb) {
DATEOFBIRTH.setCellValueFactory(new PropertyValueFactory<>("dateOfBirth"));
DATEOFBIRTH.setCellFactory(new PersonController.ColumnFormatter<>(DateTimeFormatter.ofPattern("MM/dd/yyyy")));
}
@FXML
void SubmitButton(ActionEvent event) {
if (event.getSource() == getAllData) {
try {
populatetData();
} catch (Exception ex) {
out.println(ex);
}
}
}
private class ColumnFormatter<S, T> implements Callback<TableColumn<S, T>, TableCell<S, T>> {
private final DateTimeFormatter format;
public ColumnFormatter(DateTimeFormatter format) {
super();
this.format = format;
}
@Override
public TableCell<S, T> call(TableColumn<S, T> arg0) {
return new TableCell<S, T>() {
@Override
protected void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
} else {
LocalDate ld = (LocalDate) item;
String val = ld.format(format);
setGraphic(new Label(val));
}
}
};
}
}
@SuppressWarnings("Convert2Lambda")
public void populatetData() throws Exception {
Connection conn = null;
Person pt = new Person();
conn = DBConnection.getConnection();
String SQL;
Person p = new Person();
try {
conn = DBConnection.getConnection();
data = FXCollections.observableArrayList();
SQL = "SELECT MRN, LASTNAME, DATEOFBIRTH FROM " + "HIVP.PATIENT";
out.println(SQL);
ResultSet rs = conn.createStatement().executeQuery(SQL);
if (rs != null) {
while (rs.next()) {
p.setMRN(rs.getString(1));
p.setLastName(rs.getString(2));
Date x = rs.getDate(3);
if (x != null) {
p.setDateOfBirth(rs.getDate(3).toLocalDate());
} else {
p.setDateOfBirth(LocalDate.MIN);
}
data.addAll(p);
}
Platform.runLater(new Runnable() {
@Override
public void run() {
personTable.setItems(data);
}
});
}
} catch (Exception e) {
out.println("ERROR GETTING DATA: " + e);
} finally {
if (conn != null) {
DBConnection.closeConnection((OracleConnection) conn);
}
}
}
}
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException:
file:/C:/Users/abayazids15/Documents/NetBeansProjects/IDEHMIS/dist/run1719879236/IDEHMIS.jar!/idehmis/view/Person.fxml
file:/C:/Users/abayazids15/Documents/NetBeansProjects/IDEHMIS/dist/run1719879236/IDEHMIS.jar!/idehmis/view/mainApplication.fxml:66
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.access$2700(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$IncludeElement.constructValue(FXMLLoader.java:1143)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:746)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
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 idehmis.IDEHMIS.start(IDEHMIS.java:21)
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)
... 1 more
Caused by: java.lang.NullPointerException
at idehmis.controller.PersonController.initialize(PersonController.java:58)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 23 more
Exception running application idehmis.IDEHMIS
Java Result: 1
公共类PersonController实现可初始化{
@FXML
个人桌面视图;
@FXML
私人JFXTextField Pmrn;
@FXML
私有JFXTextField名称;
@FXML
私有JFXDatePicker-Pdateofbirth;
@FXML
私有可观测列表数据;
@FXML
私有按钮getAllData;
@FXML
表列MRN;
@FXML
TableColumn LASTNAME;
@FXML
表列出生日期;
SimpleDataFormat sdf=新的SimpleDataFormat(“dd,MMM,yyyy”,Locale.getDefault());
@凌驾
公共void初始化(URL、ResourceBundle rb){
出生日期。setCellValueFactory(新的PropertyValueFactory(“出生日期”);
DATEOFBIRTH.setCellFactory(新的PersonController.ColumnFormatter(模式的DateTimeFormatter.of(“MM/dd/yyyy”));
}
@FXML
作废提交按钮(ActionEvent事件){
if(event.getSource()==getAllData){
试一试{
populatedata();
}捕获(例外情况除外){
out.println(ex);
}
}
}
私有类ColumnFormatter实现回调{
专用最终日期时间格式;
公共列格式化程序(DateTimeFormatter格式){
超级();
this.format=格式;
}
@凌驾
公共TableCell调用(TableColumn arg0){
返回新的TableCell(){
@凌驾
受保护的void updateItem(T项,布尔值为空){
super.updateItem(项,空);
如果(项==null | |空){
设置图形(空);
}否则{
LocalDate ld=(LocalDate)项;
字符串val=ld.format(格式);
设置图形(新标签(val));
}
}
};
}
}
@抑制警告(“Convert2Lambda”)
public void populateData()引发异常{
连接conn=null;
人员pt=新人员();
conn=DBConnection.getConnection();
字符串SQL;
人员p=新人员();
试一试{
conn=DBConnection.getConnection();
data=FXCollections.observearraylist();
SQL=“从“+”HIVP.PATIENT中选择MRN、姓氏、出生日期”;
out.println(SQL);
ResultSet rs=conn.createStatement().executeQuery(SQL);
如果(rs!=null){
while(rs.next()){
p、 setMRN(rs.getString(1));
p、 setLastName(rs.getString(2));
日期x=rs.getDate(3);
如果(x!=null){
p、 setDateOfBirth(rs.getDate(3.toLocalDate());
}否则{
p、 setDateOfBirth(LocalDate.MIN);
}
data.addAll(p);
}
Platform.runLater(新的Runnable(){
@凌驾
公开募捐{
personTable.setItems(数据);
}
});
}
}捕获(例外e){
println(“获取数据时出错:+e”);
}最后{
如果(conn!=null){
DBConnection.closeConnection((OracleConnection)conn);
}
}
}
}
应用程序启动方法中的异常
java.lang.reflect.InvocationTargetException
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:498)
位于com.sun.javafx.application.LaunchImpl.launchApplicationWithArgs(LaunchImpl.java:389)
位于com.sun.javafx.application.LaunchImpl.launchApplication(LaunchImpl.java:328)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:498)
在太阳发射时
<TableColumn fx:id="MRN" prefWidth="75.0" text="C1" />
<TableColumn fx:id="LASTNAME" prefWidth="75.0" text="C2" />
while (rs.next()) {
Person p = new Person();
p.setMRN(rs.getString(1));
p.setLastName(rs.getString(2));
Date x = rs.getDate(3);
if (x != null) {
p.setDateOfBirth(rs.getDate(3).toLocalDate());
} else {
p.setDateOfBirth(LocalDate.MIN);
}
data.add(p);
}
DATEOFBIRTH.setCellValueFactory(new PropertyValueFactory<>("dateOfBirth"));
DATEOFBIRTH.setCellFactory(new PersonController.ColumnFormatter<>(DateTimeFormatter.ofPattern("MM/dd/yyyy")));
private class ColumnFormatter<S, T> implements Callback<TableColumn<S, T>, TableCell<S, T>> {
private final DateTimeFormatter format;
public ColumnFormatter(DateTimeFormatter format) {
super();
this.format = format;
}
@Override
public TableCell<S, T> call(TableColumn<S, T> arg0) {
return new TableCell<S, T>() {
@Override
protected void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
} else {
LocalDate ld = (LocalDate) item;
String val = ld.format(format);
setGraphic(new Label(val));
}
}
};
}
}