title: Java Return Null When Tried to Get Method
body: <p>first of all I know this is duplicated question. But I've search and tried from <a href="https://www.google.com/search?q=java%20get%20set%20return%20null%20site:stackoverflow.com&amp;safe=strict&amp;client=firefox-b-e&amp;sxsrf=ACYBGNRTgle9cDaZQbamsqZPg3kXhUAbOg:1572294555815&amp;ei=m0-3XbCmMcK2rQGvrK6oBg&amp;start=10&amp;sa=N&amp;ved=0ahUKEwjw3-zT5b_lAhVCWysKHS-WC2UQ8NMDCJEB&amp;biw=1366&amp;bih=654" rel="nofollow noreferrer">stackoverflow</a> listed on Google to quora but still cant resolve my Get method still return null.</p>
  
  <p>This is my class <strong>loginModel.java</strong> under package com.hello.model</p>
  
  <pre><code>public class loginModel {
    public String username;
  
    public void setUsername(String username) {
        this.username = username;
    }
  
    public String getUsername() {
        return this.username;
    }
  }
  </code></pre>
  
  <p>This is my <strong>loginView.java</strong> under package com.hello.view</p>
  
  <pre><code>import com.hello.model.loginModel;
  
  public class loginView extends javax.swing.JFrame {
    loginModel login = new loginModel();
  
    public loginView() {
        initComponents();
        this.setLocationRelativeTo(null);
        loginFunction();
    }
  
    private void loginFunction(){
      String username = usernameText.getText();
      String password = passwdText.getText();
      String query = "select * from access where username = '" +username+ "' AND password = '" +password+"'";
      databaseConnect db = new databaseConnect();
  
      try (Connection con = DriverManager.getConnection(db.url, db.user, db.password);
          Statement st = con.createStatement();
          ResultSet rs = st.executeQuery(query)) {
  
          if(rs.next()) {
              if(username.equals(rs.getString("username")) &amp;&amp; password.equals(rs.getString("password"))){
                  JOptionPane.showMessageDialog(null, "login Success");
                  String name = rs.getString("name");
                  String privilege = rs.getString("privilege");
                  login.setUsername(name);
  
                  menu = new menuView();
                  menu.setVisible(true);
                  this.setVisible(false);
              }
          } else {
                  JOptionPane.showMessageDialog(null, "username or password incorrect");
              }
      } catch (SQLException e) {
          System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
  
      } catch (Exception e) {
          e.printStackTrace();
      }
    }
  }
  </code></pre>
  
  <p>I want call my username from <strong>menuView.java</strong> under package com.hello.view after login success</p>
  
  <pre><code>import com.hello.model.loginModel;
  import com.hello.view.loginView;
  
  public class menuView extends javax.swing.JFrame {    
    private String username;
    loginModel login = new loginModel();
  
    public menuView() {
      initComponents();
      this.setLocationRelativeTo(null);
      initMenu();
    }
  
    private void initMenu(){
      username = login.getUsername();
      JOptionPane.showMessageDialog(this, username);
    }
  }
  </code></pre>
  
  <p>As per my question when I call Get method from loginModel, messagebox return null.</p>
  
  <p>I've tried:</p>
  
  <ol>
  <li>Put system.out.println directly in <strong>loginModel.java</strong>, value return
  and call system.out.println in <strong>menuView.java</strong> at the same time but value return null. How?</li>
  <li>Send string between jframe with menu = menuView(username) in <strong>loginView.java</strong> and retrieve in <strong>menuView.java</strong>, value return null</li>
  <li>Using no model and create set string in <strong>loginView</strong> and call it in
  <strong>menuView</strong>, value return null</li>
  </ol>
  
  <p>I need values that I want to use in another class/package/jframe. Am I doing wrong?</p>