当前位置: 首页 > 知识库问答 >
问题:

Getter值持续返回空Java

佟阳焱
2023-03-14
test: entity.account.Admin@18a607c2

WHY YOU ARE NOT WORKING?! null
 public class AdminMasterPanel extends JPanel {

    protected JFrame myFrame;

    private Admin admin;


    public Admin getAdmin() {
        return admin;
    }

    public void setAdmin(Admin admin) {
       this.admin = admin;
       System.out.println("test: "+this.admin);
    }


    /**
     * Create the panel.
     */
    public AdminMasterPanel(JFrame mf) {

       // Set the frame to the program
       myFrame = mf;
       setLayout(null);
       setBounds(0, 0, 1280, 720);

       System.out.println("WHY YOU ARE NOT WORKING?! "+getAdmin());


     }
}
AdminMasterPanel amp = new AdminMasterPanel(null); 
amp.setAdmin(admin);

共有1个答案

廖臻
2023-03-14

因为您在setadmin()之前调用getadmin()。您的构造函数甚至没有获得admin对象。因此,您基本上没有使用getAdmin()调用任何内容。

你要做的是:

public AdminMasterPanel(JFrame mf, Admin admin) {

   // Set the frame to the program
   myFrame = mf;
   setLayout(null);
   setBounds(0, 0, 1280, 720);
   this.admin = admin;
   System.out.println("WHY YOU ARE NOT WORKING?! "+getAdmin());
 }

或者您似乎希望使用对象方法setadmin()设置管理。您必须删除构造函数中的sysout

 类似资料: