Search code examples
javastringswingnullpointerexceptionjlabel

A method with return type String that works for System.out.println(), but not if I want to use the created String in a JLabel


Okay so here's the deal... I have a static object of type Student, named workingStudent. Student has a method to return the student's name as a String.

I have a JFrame with a CardLayout JPanel that has 2 cards. card 1 is a login screen, and upon successful login, sets workingStudent to whatever object was associated with the users account via this code..

private void validateLogin(ArrayList<Student> students){
    boolean valid = false;

    for(int i = 0; i < students.size(); i++){

        if(username.getText().equals(students.get(i).getUsername())
                && password.getText().equals(students.get(i).getPassword()))
        {   
            valid = true;
            setWorkingStudent(students.get(i));
            currentSemester=(students.get(i).getLastSemester());
            System.out.println("Successful Login!");
            cl.show(cards, HOMEPANEL);
            System.out.println(workingStudent.getName());
        }
    }
    if(valid == false){
        System.out.println("Invalid Login, try again");
    }

}

This appears to be working just fine. When this method is run, it prints out the workingStudents name with no errors. Which leads me to believe that it also sets workingStudent without any errors as well.

The problem appears in the following code, where workingStudent.getName() should be added to the JLabel's text.

 private JPanel homePanel() {
    JPanel home = new JPanel();
    home.setLayout(null);
    home.setBackground(Color.DARK_GRAY);

    JLabel hi = new JLabel("Hello, "+workingStudent.getName());
    hi.setSize(400, 100);
    hi.setLocation(10,10);
    hi.setFont(new Font("Serif", Font.BOLD, 36));
    hi.setForeground(Color.WHITE);

    home.add(hi);

    return home;

}

But instead, I get a NullPointerException. I'm looking for some help/any possible explanation of what could be wrong.

How is it that getName() works just fine in validateLogin() but not for homePanel()??

Any help would be greatly appreciated.

Oh, and this is where I use validateLogin (it responds to a button click)

public void actionPerformed(ActionEvent e) {
    if (e.getSource()==login){
        System.out.println("Logging in...");
        validateLogin(students);
    }
    else if (e.getSource()==register){

    }
}

Solution

  • It looks like your program invokes homePanel method before workingStudent variable created. Try modify your code this way:

    private JPanel homePanel() {
        JPanel home = new JPanel();
        home.setLayout(null);
        home.setBackground(Color.DARK_GRAY);
        String greeting = 
          workingStudent==null?"Hello, unknown user":"Hello, "+workingStudent.getName();
        JLabel hi = new JLabel(greeting);
        hi.setSize(400, 100);
        hi.setLocation(10,10);
        hi.setFont(new Font("Serif", Font.BOLD, 36));
        hi.setForeground(Color.WHITE);
    
        home.add(hi);
    
        return home;
    
    }