问题
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