So I am trying to create a javafx program where I can sign up as a patient. I don't know how to save data yet so I will be using an array list. I have declared an arraylist of patient on the sign up controller class and created a method to store data. But whenever I try to run the program I always get the "Find why this could be null". I don't know what is the reason behind this.
Here is my code:
public void signUpClicked() throws IOException {
boolean isEmpty = false;
if(namePt.getText().equals("")){
namePt.setStyle("-fx-border-color: red; -fx-border-width: 2px;");
signUpWarningPt.setText("Incomplete information!");
isEmpty=true;
}
else {
signUpWarningPt.setStyle(null);
namePt.setStyle(null);
}
if(agePt.getText().equals("")){
agePt.setStyle("-fx-border-color: red; -fx-border-width: 2px;");
signUpWarningPt.setText("Incomplete information!");
isEmpty=true;
}
else {
signUpWarningPt.setStyle(null);
agePt.setStyle(null);
}
if(genderPt.getText().equals("")){
genderPt.setStyle("-fx-border-color: red; -fx-border-width: 2px;");
signUpWarningPt.setText("Incomplete information!");
isEmpty=true;
}
else {
signUpWarningPt.setStyle(null);
genderPt.setStyle(null);
}
if(addressPt.getText().equals("")){
addressPt.setStyle("-fx-border-color: red; -fx-border-width: 2px;");
signUpWarningPt.setText("Incomplete information!");
isEmpty=true;
}
else {
signUpWarningPt.setStyle(null);
addressPt.setStyle(null);
}
if(emailPt.getText().equals("")){
emailPt.setStyle("-fx-border-color: red; -fx-border-width: 2px;");
signUpWarningPt.setText("Incomplete information!");
isEmpty=true;
}
else {
signUpWarningPt.setStyle(null);
emailPt.setStyle(null);
}
if(usernamePt.getText().equals("")){
usernamePt.setStyle("-fx-border-color: red; -fx-border-width: 2px;");
signUpWarningPt.setText("Incomplete information!");
isEmpty=true;
}
else {
signUpWarningPt.setStyle(null);
usernamePt.setStyle(null);
}
if(passwordPt.getText().equals("")){
passwordPt.setStyle("-fx-border-color: red; -fx-border-width: 2px;");
signUpWarningPt.setText("Incomplete information!");
isEmpty=true;
}
else {
signUpWarningPt.setStyle(null);
passwordPt.setStyle(null);
}
if(confirmPasswordPt.getText().equals("")){
confirmPasswordPt.setStyle("-fx-border-color: red; -fx-border-width: 2px;");
signUpWarningPt.setText("Incomplete information!");
isEmpty=true;
}
else {
signUpWarningPt.setStyle(null);
confirmPasswordPt.setStyle(null);
}
if(!isEmpty && checkPassword(passwordPt.getText(),confirmPasswordPt.getText())){
recordLoginInfo(usernamePt.getText(),passwordPt.getText(),"PatientLoginInfo.txt");
addPatientInfo(namePt.getText(),agePt.getText(),genderPt.getText(),addressPt.getText(),usernamePt.getText(),passwordPt.getText());
setDetails();
}
}
This is the variables I created:
static ArrayList<Patient> patientList;
@FXML private TextField namePt;
@FXML private TextField agePt;
@FXML private TextField genderPt;
@FXML private TextField addressPt;
@FXML private TextArea concernsPt;
@FXML private TextField emailPt;
@FXML private TextField usernamePt;
@FXML private TextField passwordPt;
@FXML private TextField confirmPasswordPt;
@FXML private Button signupBtnPt;
@FXML private Button closeBtnPt;
@FXML private Hyperlink patientLoginLink;
@FXML private Label signUpWarningPt;
This is the method I created for adding data on the arraylist:
public void addPatientInfo(String name, String age, String gender, String address,String username, String password){
Patient p = new Patient(name,age,gender,address,username,password);
patientList.add(p);
}
Here is the patient class:
public class Patient implements Serializable {
private String name;
private String age;
private String gender;
private String address;
private String username;
private String password;
public Patient(String name, String age, String gender, String address, String username, String password) {
this.name = name;
this.age = age;
this.gender = gender;
this.address = address;
this.username = username;
this.password = password;
}
}
And lastly the error message I always get:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1862)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1729)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8889)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3856)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1851)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2584)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:831)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1859)
... 46 more
Caused by: java.lang.NullPointerException: Cannot invoke "java.util.ArrayList.add(Object)" because "Controllers.PatientSignupController.patientList" is null
at Controllers.PatientSignupController.addPatientInfo(PatientSignupController.java:118)
at Controllers.PatientSignupController.signUpClicked(PatientSignupController.java:111)
... 57 more