子类尝试使用对象时,该对象具有空值.为什么? [英] Object has null values when subclass attempts to use it. Why?

查看:114
本文介绍了子类尝试使用对象时,该对象具有空值.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,所以我可能整个事情都做错了.我必须为软件工程课程做这个庞大的项目.该代码长约2,000行,所以这是基本代码

I'm a newbie Java guy so I'm probably doing this entire thing completely wrong. I have to do this giant project for software engineering class. The code is about 2,000 lines long so this is skeleton code

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内部访问currentClient?为这个JLabel说:

I already am pretty sure I am doing this COMPLETELY wrong, however my question is why can't I access currentClient inside of ClientInfoPanel? Say for this JLabel:

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

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

The ClientInfoPanel recognizes currentClient exists and that the getUsername() method exists, however it prints:

当前登录为:"

"Currently logged in as: "

推荐答案

您显示的代码看起来不错,所以问题出在其他地方,或者该代码不代表您拥有的代码.另外,您将成功访问currentClient,除非您遇到NullPointerException异常或将其捕获到某处,否则.getUsername()调用将得以解决.所以问题实际上出在.getUsername()上,以某种方式在调用.getUsername()时用户名未初始化.

The code you showed looks fine, so the problem is somewhere else, or this code is not representative of what you have. Also, you ARE accessing currentClient successfully, unless you're getting a NullPointerException or catching it somewhere, then the .getUsername() call IS resolving. So the problem is actually with .getUsername(), somehow username is uninitialized when you call .getUsername();

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

As a test, can you call .setUsername() on currentClient right before you call .getUsername()? That should help us narrow down the problem, isolate it to either the class access or the variable being initialized.

此外,您知道如何使用断点进行调试吗?您提到自己是新手,所以您可能不知道这是可能的.如果您使用的是Eclipse(或其他优秀的IDE),则可以设置断点并在DEBUG构建中运行该程序,那么该程序将在到达设置断点的行时冻结,并且您可以观察程序逐行移动行,并在程序更新变量时查看它们. Google Java调试教程. :)

Also, do you know how to debug with breakpoints? You mentioned you're new, so you might not know this is possible. If you're using Eclipse (or another good IDE) you can set breakpoints and run the program in DEBUG build, then the program will freeze when it hits the line you set a breakpoint on, and you can watch as the program moves line by line, and see the variables as they are updated by the program. Google java debug tutorial. :)

这篇关于子类尝试使用对象时,该对象具有空值.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆