0

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;
    }
DRT
  • 93
  • 2
  • 8
  • 1
    "*`private static User currentUser;`*" - if you do not guarantee to initialize this value before the getter is called for the first time, the getter will return `null`. I suspect that this is the reason for the `NullPointerException`. – Turing85 Jun 14 '18 at 22:46
  • Did you initialize your LoginControl class anywhere? Is it the culprit, or something it is attempting to call? – Phil Freihofner Jun 14 '18 at 22:46
  • I read the answer that this was marked as a duplicate of - as well as other documentation. I understand what causes a NPE, I just couldn't find the source of it in my code and was asking for assistance. Needless to say I located the problem, I just had to swap 2 lines of code in my LoginContol class. – DRT Jun 15 '18 at 00:16
  • I can't answer this as it was marked 'duplicate' but these 2 lines had to have their order swapped -SceneManager.getCalendarScene(); setCurrentUser(user); – DRT Jun 15 '18 at 00:18
  • In general it's a bad idea to retrieve data in a static initializer. This data is retrieved when the class is loaded resulting in a weird dependency. E.g. you also want to allow the user to logout and another user to login, replacing the current user. `CalendarControl` will never see the new user in it's `currentUser` field and always keep the one set as field value the first time.... – fabian Jun 15 '18 at 05:56

0 Answers0