Can someone check my code? I am having my calculation for the score using TextView and EditText, i also declared Integers for the variables, but when i run my code, it gives me error when parsing integer. Thanks in advance. Also check my Log cat for more info. Here’s my code:
initControls();
}
private void initControls()
{
amount1=(EditText)findViewById(R.id.amount1);
amount2=(EditText)findViewById(R.id.amount2);
amount3=(EditText)findViewById(R.id.amount3);
result=(TextView)findViewById(R.id.result);
calculate=(Button)findViewById(R.id.finish);
calculate.setOnClickListener(new Button.OnClickListener()
{public void onClick
(View v) { calculate();}});
}
private void calculate()
{
if(amount1.getText().toString().equals(null))
{
x=0;
}
else
amount1.getText().toString();
x=Integer.parseInt(amount1.getText().toString());
if(amount2.getText().toString().equals(null)) {
y=0;
}
else
amount2.getText().toString();
y=Integer.parseInt(amount2.getText().toString());
if(amount3.getText().toString().equals(null)) {
v=0;
}
else
amount3.getText().toString();
v=Integer.parseInt(amount3.getText().toString());
{
z=x+y+v;
result.setText(Integer.toString(z));
}
Here’s my Log cat:
08-16 16:15:51.794: W/dalvikvm(224): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
08-16 16:15:51.794: E/AndroidRuntime(224): Uncaught handler: thread main exiting due to uncaught exception
08-16 16:15:51.824: E/AndroidRuntime(224): java.lang.NumberFormatException: unable to parse '' as integer
08-16 16:15:51.824: E/AndroidRuntime(224): at java.lang.Integer.parseInt(Integer.java:353)
08-16 16:15:51.824: E/AndroidRuntime(224): at java.lang.Integer.parseInt(Integer.java:323)
08-16 16:15:51.824: E/AndroidRuntime(224): at drj.thesis.tridi.Maingame.calculate(Maingame.java:70)
08-16 16:15:51.824: E/AndroidRuntime(224): at drj.thesis.tridi.Maingame.access$0(Maingame.java:54)
08-16 16:15:51.824: E/AndroidRuntime(224): at drj.thesis.tridi.Maingame$1.onClick(Maingame.java:52)
08-16 16:15:51.824: E/AndroidRuntime(224): at android.view.View.performClick(View.java:2364)
08-16 16:15:51.824: E/AndroidRuntime(224): at android.view.View.onTouchEvent(View.java:4179)
08-16 16:15:51.824: E/AndroidRuntime(224): at android.widget.TextView.onTouchEvent(TextView.java:6541)
08-16 16:15:51.824: E/AndroidRuntime(224): at android.view.View.dispatchTouchEvent(View.java:3709)
08-16 16:15:51.824: E/AndroidRuntime(224): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-16 16:15:51.824: E/AndroidRuntime(224): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-16 16:15:51.824: E/AndroidRuntime(224): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-16 16:15:51.824: E/AndroidRuntime(224): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-16 16:15:51.824: E/AndroidRuntime(224): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-16 16:15:51.824: E/AndroidRuntime(224): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
08-16 16:15:51.824: E/AndroidRuntime(224): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
08-16 16:15:51.824: E/AndroidRuntime(224): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
08-16 16:15:51.824: E/AndroidRuntime(224): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
08-16 16:15:51.824: E/AndroidRuntime(224): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
08-16 16:15:51.824: E/AndroidRuntime(224): at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 16:15:51.824: E/AndroidRuntime(224): at android.os.Looper.loop(Looper.java:123)
08-16 16:15:51.824: E/AndroidRuntime(224): at android.app.ActivityThread.main(ActivityThread.java:4363)
08-16 16:15:51.824: E/AndroidRuntime(224): at java.lang.reflect.Method.invokeNative(Native Method)
08-16 16:15:51.824: E/AndroidRuntime(224): at java.lang.reflect.Method.invoke(Method.java:521)
08-16 16:15:51.824: E/AndroidRuntime(224): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-16 16:15:51.824: E/AndroidRuntime(224): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-16 16:15:51.824: E/AndroidRuntime(224): at dalvik.system.NativeStart.main(Native Method)
08-16 16:15:51.857: I/dalvikvm(224): threadid=7: reacting to signal 3
08-16 16:15:51.864: E/dalvikvm(224): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
The problem is fairly clear:
You’re trying to parse an empty string. You need to work out why you’re trying to parse an empty string.
I suspect this is a large part of it:
You should have braces around your
elseblock so that both statements are in it:… although the first statement in the
elseblock looks pointless to me.I’d actually expect
amount1.getText()to already return a string though, making thetoString()call unnecessary (maybe not; this could be an Android facet I don’t know about) but I’d never expect the result to be equal to null. Perhaps you meant to compare with “” like this:(You can use
amount1.getText().toString().getLength() == 0to check for an empty string too. It’s a matter of preference which you pick.)You’ve got the same problem just below this as well, suggesting you should probably extract a method to perform this conditional parsing.
Additionally, you should really fix your indentation. Indenting code properly makes a huge difference to code readability.