As far as I know, I put in the correct try catch statements to stop the NumberFormatException…the NullPointerException may be coming from this:
TextView tvfin = null;
tvfin.setText(fin+"");
tvfin = (TextView)findViewById(R.id.tvfinalgrade);
Those three lines are going to be in an OnClickListener but I haven’t created it yet, I’m not sure if that could cause a problem?
Any help is much appreciated!
package wilson.GC;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
public class GFActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.getfinal);
double q1, q2, ex, fin;
EditText etq1, etq2, eteg;
etq1 = (EditText)findViewById(R.id.editText1);
try{
q1 = Double.parseDouble(etq1.getText().toString());
} catch (NumberFormatException e) {
q1=0;
}
etq2 = (EditText)findViewById(R.id.editText2);
try{
q2 = Double.parseDouble(etq2.getText().toString());
} catch (NumberFormatException e){
q2 = 0;
}
eteg = (EditText)findViewById(R.id.editText3);
try{
ex = Double.parseDouble(eteg.getText().toString());
} catch (NumberFormatException e){
ex = 0;
}
fin = 0.4*q1+0.4*q2+0.2*ex;
if(fin == (int)fin){
System.out.println((int)fin);
}
else{
fin = 0.01*((int)(fin*100));
System.out.println(fin);
}
TextView tvfin = null;
tvfin.setText(fin+"");
tvfin = (TextView)findViewById(R.id.tvfinalgrade);
}
}
Try setting tvfin before setting its text: