Java Get / Set方法返回null

时间:2013-12-31 08:52:14

标签: java get null set elm327

我想获取其他类(​​jframe)的变量,但get方法返回null ..但是set方法正在运行..

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

我添加了初始化的案例..当我运行getElm()时,返回null ...我该怎么办?

    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 个答案:

答案 0 :(得分:2)

你需要初始化Elm32x.so部分代码将是

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