我正在尝试学习Java,并且正在尝试制作一个简单的计算器。出于某种原因NullPointerException,我的TextField.setText().
这是我的代码:
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CalcUI().setVisible(true);
}
});
Calc c = new Calc();
c.setVals(2,2,'+');
result = c.doCalc();
//need to setText(String.valueOf(c.doCalc()))
txtScreen.setText(""+result);
System.out.println(result);
}
在我的第二课 Calc 中:
//sets values from calc GUI to local class vars
public void setVals(double n1, double n2, char c){
NUM1=n1;
NUM2=n2;
CHAR=c;
}
//do the math
public double doCalc(){
switch (CHAR){
case '+':
RESULT = NUM1+NUM2;
break;
}
return RESULT;
}
好的...所以我发送它values(2,2),它然后c.doCalc()返回4。我的System.out.println(result)打印4但我txtScreen.setText(""+result);导致空指针异常。
有什么帮助吗?