public class welcomepage extends javax.swing.JFrame {
    backendcode bec;
    String username;
    public welcomepage() {
            initComponents();
            username=null;
            backendcode bec= new backendcode("dummy");
            System.out.println("bec created "+ bec);
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            System.out.println("bec created "+ bec);
            bec.back_login_credentials(username);
    }
    /*
    and other private methods(not mentioned here) which also needs to access bec object
    */
}

 public class backendcode {
     public backendcode(String dummy) {
           //some code
     }
     public void back_login_credentials(String username, String password) {
          //some code
     }
  }


我已经声明了bec(backendcode对象,以便整个类都可以看到)作为welcomepage类内的成员数据,并在其construcor中初始化
但会在该构造函数中通过一些初始化创建它,但是jButton1ActionPerformed方法中的bec对象的值将为null。为什么呢?。获得该实例化对象实例的解决方案是什么?

最佳答案

全局bec变量永远不会初始化。您在构造函数的范围内创建一个新的bec变量。

请尝试以下操作:

public class welcomepage extends javax.swing.JFrame {
    backendcode bec;
    String username;
    public welcomepage() {
    initComponents();
            username=null;
            bec= new backendcode("dummy");
    ...

10-07 23:38