【问题标题】:Object has null values when subclass attempts to use it. Why?当子类尝试使用对象时,对象具有空值。为什么?
【发布时间】:2012-05-01 21:09:25
【问题描述】:

我是 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);
   }

}

我已经很确定我这样做完全错了,但是我的问题是为什么我不能在 ClientInfoPanel 中访问 c​​urrentClient?为这个 JLabel 说:

infoPanelUserName.setText("Currently logged in as: " + currentClient.getUsername());

ClientInfoPanel 识别出 currentClient 存在并且 getUsername() 方法存在,但是它会打印:

"当前登录为:"

【问题讨论】:

  • 请显示您正在讨论的代码(“当我在 ClientInfoPanel 中调用 currentClient 的 get 方法时,它可以识别变量但它不包含正确的值”)。
  • 所以你是一个初学者,你一次写了2000行代码,然后你尝试运行它,它根本不起作用,现在它太复杂了,你无法调试。嗯......也许问题不在于您的代码,而在于您编写它的方式。即使您修复了此错误,也可能还有 100 个其他错误。
  • 我的意思是 infoPanelFullName.setText("Full Name: " + currentClient.getFullname());它识别 currentClient 和方法 getFullname() 存在。然而,JLabel 只显示“全名:”,调试显示 getFullname() 返回“”,这是默认构造函数...
  • 是的,Mark Byers,我意识到我可能没有正确地写这个。我是新手,但是我的教授希望并要求完成整个项目,而我对 Java 的经验很少,所以我必须即时学习。到目前为止,运行程序已经完全正常,这是因为其他面板没有将信息打印到屏幕上。
  • @SeanMG:为避免混淆,请您编辑您的问题以准确说明您的意思。您应该强烈考虑制作一个简短的完整测试用例,我们可以编译和运行(参见sscce.org)。 “短”是指“少于 20 行”。

标签: java class client superclass


【解决方案1】:

您显示的代码看起来不错,所以问题出在其他地方,或者此代码不能代表您所拥有的。此外,您正在成功访问 currentClient,除非您收到 NullPointerException 或在某处捕获它,否则 .getUsername() 调用正在解析。所以问题实际上出在 .getUsername() 上,当你调用 .getUsername() 时,不知何故用户名未初始化;

作为测试,您可以在调用 .getUsername() 之前在 currentClient 上调用 .setUsername() 吗?这应该有助于我们缩小问题范围,将其隔离到类访问或正在初始化的变量。

另外,你知道如何使用断点进行调试吗?你提到你是新人,所以你可能不知道这是可能的。如果您使用的是 Eclipse(或其他好的 IDE),您可以设置断点并在 DEBUG 构建中运行程序,然后程序将在遇到您设置断点的行时冻结,并且您可以观察程序移动行行,并查看程序更新的变量。谷歌java调试教程。 :)

【讨论】:

    【解决方案2】:

    我同意(不确定我是否应该添加,因为其他人已经回答了,礼仪是什么?)。我运行得很好,它向我显示了一个面板,其中用户以我的身份登录,如 BookRental 构造函数中设置的那样:

    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    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 String username;
         private void setUsername(String u){
          username = u;
         }
         public String getUsername(){
          return username;
         }
       }
    
    
       public class ClientInfoPanel extends JPanel{      //Panel showing main menu.
          //At this point currentClient will contain values
          public ClientInfoPanel(){
             initComponents();
          }
          private void initComponents(){
             infoPanelUserName = new JLabel();
             infoPanelUserName.setText("Currently logged in as: " + currentClient.getUsername());
             this.add(infoPanelUserName);
          }
          private JLabel infoPanelUserName;
        }
       public ClientInfoPanel clientInfoPanel;
    
    
       //Constructor
       public BookRental(){
           currentClient.setUsername("Me");
           initComponents();
       } 
       private void initComponents(){
           clientInfoPanel = new ClientInfoPanel();
              this.add(clientInfoPanel);
              clientInfoPanel.setVisible(true);
       }
    
       public static void main(String args[]){
    
           new BookRental().setVisible(true);
       }
     }
    

    【讨论】:

    • 是的,变量获取了一个值,但是当您调用 JLabel 时,它无法显示该值。 Marko Topolnik 的回答让我相信我创建的类是嵌套的,并且值位于内部类无法访问的外部类中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-15
    • 2014-11-07
    相关资源
    最近更新 更多