Object has null values when subclass attempts to u

2019-07-18 14:15发布

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: "

2条回答
孤傲高冷的网名
2楼-- · 2019-07-18 14:46

I agree (not sure whether I should add since someone else already answered, what is the etiquette?). I ran this fine and it showed me a panel with the user logged in as Me as set in the BookRental constructor:

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);
   }
 }
查看更多
女痞
3楼-- · 2019-07-18 14:55

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

查看更多
登录 后发表回答