Java swing getters方法返回空值

时间:2018-11-26 16:45:32

标签: java swing user-interface model-view-controller nullpointerexception

你好我有这个问题

这是我的查看代码

public class UserMainView extends javax.swing.JFrame {

User Authuser;
UserMainViewController userMainViewController;

/**
 * Creates new form UserMainView
 */
public UserMainView(User user) {
    this.Authuser=user;
    userMainViewController = new UserMainViewController(this.Authuser, this);
    userMainViewController.initView();
    initComponents();
}

public void setUser(User user){
    this.Authuser=user;

}

public JLabel getWelcomeLabel() {
    return WelcomeLabel;
}

welcomeLabel在UserMainView.java中定义

 /**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    WelcomeLabel = new javax.swing.JLabel();
    logoutButton = new javax.swing.JButton();
    UpdateButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    WelcomeLabel.setFont(new java.awt.Font("Times New Roman", 2, 18)); // NOI18N
    WelcomeLabel.setText("Welcome Mr.");

还有更多代码,但它只是框架设计,因此不重要

这是控制器代码

public class UserMainViewController {
User userModel;
UserMainView userMainView;

public UserMainViewController(User userModel, UserMainView userMainView) {
    this.userModel = userModel;
    this.userMainView = userMainView;
}

public void initView(){
    setLabelText();

}


private void setLabelText(){

    userMainView.getWelcomeLabel().setText("Welcome Mr."+this.userModel.getName()); // i get null exception here

}

在这里我将调用方法

private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
    try {
        int authUserId = loginController.loginButtonAction();
        user= userManger.getUser(authUserId);
        if(user !=null){
            loginController.hideLoginView();
            new UserMainView(user).setVisible(true);
        }

    } catch (SQLException ex) {
        Logger.getLogger(LoginView.class.getName()).log(Level.SEVERE, null, ex);
    }
}          

我在setLabelText方法中获得null异常,如果我打印getWelcomeLabel(),则它返回null值

出什么问题了?

0 个答案:

没有答案