Forgive me for my ignorance, but I am not understanding why my object is null. I have been successfully refering to my getter previously. I just added this getter to my initialize() method and it is throwing a NullPointer Exception. When debugging the stack trace shows that it is this getter that isn't assigning a value to my object.
I don't understand why it works sometimes and now it doesn't. I just created a static final instance of it and that gives me the same null result. I am a student completing a project for school.
Below is a snippet from my CalendarControl class that is throwing the exception:
private List<Appointment> appointments;
static final User currentUser = LoginControl.getCurrentUser();
//region @init
@Override
public void initialize(URL location, ResourceBundle resources) //from Initializable
{
System.out.println(currentUser.toString());
try {
appointments = SQLAppointmentDAO.selectAppointment(currentUser);
} catch (SQLException e) {
e.printStackTrace();
}
for (Appointment appointment : appointments)
{
utilities.Timer.addAppointmentTimer(appointment);
}
The code that is causing me problems is the
static final User currentUser = LoginControl.getCurrentUser();
This is a snippet from the LoginControl class:
private static User currentUser;
public void login() throws IOException
{
String username = txtUsername.getText();
String password = pwdPassword.getText();
SQLUserDAO userSQL = new SQLUserDAO();
User user;
user = userSQL.getUserByUsername(username);
if (user == null)
{
badLogon();
}
else if (user.verify(password))
{
SceneManager.getMainStage().close();
SceneManager.getCalendarScene();
setCurrentUser(user);
loginLog();
}
else
{
badLogon();
}
}
private void loginLog()
{
File loginLogFile = new File(System.getProperty("user.dir") + "/login_log.txt");
try
{
loginLogFile.createNewFile();
FileWriter writer = new FileWriter(loginLogFile, true);
String log = getCurrentUser().getUsername() + " successfully logged in at " + LocalDateTime.now().format(
DateTimeFormatter.ISO_LOCAL_DATE_TIME) + "\n";
writer.append(log);
writer.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static User getCurrentUser()
{
return currentUser;
}
private static void setCurrentUser(User user)
{
currentUser = user;
}