I followed this post to accomblish Date formatting, but got java.lang.NullPointerException
Controller Class:
Here is my code: The FXML File:
<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>
Here is the Person Class:
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;
}
}
And Here is the Controller:
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
What did I do wrong to get NullPointException. Please Help.Thanks