Java Get/Set method returns null

江枫思渺然 提交于 2019-12-13 05:58:20

问题


I want to get variable other class(jframe) but get method returns null.. But set method is running..

public class FrameGauges extends javax.swing.JFrame {
    Elm32x elma=null;
    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
       FrameBaglanti a = new FrameBaglanti();
       elma = a.GetElm();
       System.out.println(elma);
    }
}

I added initialized case.. When I run getElm(), that returns null... What should i do ?

    public class FrameBaglanti extends javax.swing.JFrame {
    Elm32x elm;    
    public Elm32x GetElm() {
      return elm;
    }

    public void SetElm(Elm32x elm) {
      this.elm = elm;
      System.out.println("I got " + elm );
    }

    public void ElmConnect() throws Exception {
    elm=null;
    elm = new Elm32x(cboxPort.getSelectedItem().toString());
    elm.connect();
    elmCon = true;
    SetElm(elm);
    }

    private void ButtonBaglanActionPerformed(java.awt.event.ActionEvent evt) {                                             
      if (ButtonBaglan.isSelected()) {
        try {
            // Init the device
            if (elmCon == false) {
                ElmConnect();
            }
        } catch (Exception e) {
            System.out.println("Baglanamadi..");
            ButtonBaglan.setSelected(false);
        }
       }else {
          elm.disconnect();
             }
     }
}

回答1:


You need to initialize Elm32x.so the part of code will be

public class FrameBaglanti extends javax.swing.JFrame {
Elm32x elm = new Elm32x(); 
//remaining code


来源:https://stackoverflow.com/questions/20854514/java-get-set-method-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!