我是一个新手Java人,所以我可能做这件事完全错误。我必须做这个软件工程类的巨大项目。该代码是约2000线长,所以这是骨架代码当子类尝试使用它时,对象具有空值。为什么?
public class BookRental extends JFrame{
public Client currentClient = new Client(); // creating client object
//rest of declared variables.
public class Client{ //Client class containing all get/set methods for each variable
private username;
private void setUsername(String u){
username = u;
}
public String getUsername(){
return username;
}
public class LoginPanel extends JPanel{} //Panel to show and receive login info.
public class RegisterPanel extends JPanel{} //Panel to register.
public class MenuPanel extends JPanel{ //Panel showing main menu.
//At this point currentClient will contain values
public ClientInfoPanel(){
initComponents();
}
private void initComponents(){
infoPanelUserName = new JLabel();
infoPanelFullName.setText("Currently logged in as: " + currentClient.getUsername());
}
private JLabel infoPanelUserName;
}
public class ClientInfoPanel extends JPanel{} //Panel to print currentClient info to screen using JLabel objects
private void ViewClientInfoButtonActionPerformed(event){ // Using button in Menu Panel to setVisibility of clientInfoPanel to (true)
//At this point there will be a value for currentClient
clientInfoPanel = new ClientInfoPanel();
this.add(clientInfoPanel);
menuPanel.setVisible(false);
clientInfoPanel.setVisible(true);
}
public BookRental(){initComponents();} //Constructor
private void initComponents(){} // Creates all panels and sets visibility off, besides login
public static void main(String args[]){
new BookRental().setVisible(true);
}
}
我已经敢肯定,我这样做完全错误的,但我的问题是,为什么我不能访问currentClient ClientInfoPanel内?说这个的JLabel:
infoPanelUserName.setText("Currently logged in as: " + currentClient.getUsername());
的ClientInfoPanel承认currentClient存在并且getUsername()方法存在,但它打印:
“目前登录为:”
请出示你在谈论(“当我调用get方法currentClient里面的代码ClientInfoPanel,它识别变量,但它不包含正确的值“)。 –
所以你是一个初学者,你一次写了2000行代码,然后你试图运行它,它根本不起作用,现在它太复杂了,你无法进行调试。嗯......也许问题不在于你的代码,而在于你写这些代码的方式。即使您修复此错误,也可能有100个其他错误。 –
我是指infoPanelFullName.setText(“Full Name:”+ currentClient.getFullname());它认识到currentClient和方法getFullname()存在。然而,JLabel只显示“Full Name:”并且调试显示getFullname()返回“”这是默认的构造函数... – SeanMG