复数:为什么这不起作用?

时间:2014-05-31 01:48:16

标签: java android complex-numbers

当我加载此代码并输入一些数字并尝试添加复数时,它会强制关闭应用程序。

public void complex3(View v){

    EditText numAA=(EditText)findViewById(R.id.complextest1);
    EditText numBB=(EditText)findViewById(R.id.complextest2);
    EditText numCC=(EditText)findViewById(R.id.complextest3);
    EditText numDD=(EditText)findViewById(R.id.complextest4);
    Double num1=Double.parseDouble(numAA.getText().toString()) ,
            num2=Double.parseDouble(numBB.getText().toString()),
            num3=Double.parseDouble(numCC.getText().toString()),
            num4=Double.parseDouble(numDD.getText().toString());
    Complex a = new Complex(num1,num2);
    Complex b = new Complex(num3,num4);
    Complex c = Complexadd(a,b);
    TextView X=(TextView)findViewById(R.id.complexanswertest);
    X.setText("X= "+new DecimalFormat("##.##").format(c));
}

private Complex Complexadd(Complex a, Complex b) {
    // TODO Auto-generated method stub
    return null;
}

1 个答案:

答案 0 :(得分:1)

您显然会得到NullPointerException

下面

Complex c = Complexadd(a,b);<------- C will be null

X.setText("X= "+new DecimalFormat("##.##").format(c));<----NPE

因为C是空的。

  private Complex Complexadd(Complex a, Complex b) {
        // TODO Auto-generated method stub
        return null;//Do addition of Complex Numbers and Return Complex here
    }