Search code examples
javaclassclientsuperclass

Object has null values when subclass attempts to use it. Why?


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

}

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());

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

"Currently logged in as: "


Solution

  • 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();

    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.

    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. :)