-5

I've been debugging my app with my phone and all the logcat errors I get refer to line 54 in my activity where I parse a String into an Int. The basic idea of the app is a penny converter in which the user enters the number of pennies they wants to convert and is divided from quarters down to the remainder pennies. At this point I'm not sure if I'm properly catching the event and have gone back and forth on using an anonymous inner class and just implementing in the class.

Here's the code for the java app:

public class PennyConverterActivity extends Activity
{

    EditText et;
    TextView tv;

    int cents;
    int remaining;
    int quarters;
    int dimes;
    int nickels;
    int pennies;
    String result;

    @Override

    public void onCreate(Bundle b)
    {
        super.onCreate(b);
        setContentView(R.layout.main);

        et = (EditText) findViewById(R.id.penny);

        et.setText(result);

        et.setOnKeyListener(new View.OnKeyListener() 
        {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) 
            {
                if((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER))
                {
                    result = et.getText().toString();
                    return (true);
                }

                return (true);
            }
        }); 


        cents = Integer.parseInt(result); //LogCat error refers to this line

        tv = (TextView) findViewById(R.id.quarters);

        quarters = cents / 25;
        tv.setText(R.string.quarters);
        remaining = cents % 25;

        tv = (TextView) findViewById(R.id.dimes);

        dimes = remaining / 10;
        tv.setText(R.string.dimes);
        remaining = remaining % 10;

        tv = (TextView) findViewById(R.id.nickels);

        nickels = remaining / 5;
        tv.setText(R.string.nickles);

        tv = (TextView) findViewById(R.id.pennies);

        pennies = remaining % 5;
        tv.setText(R.string.pennies);

    }

}

and my Logcat errors

02-26 15:26:30.602: E/AndroidRuntime(25020): FATAL EXCEPTION: main
02-26 15:26:30.602: E/AndroidRuntime(25020): java.lang.RuntimeException: Unable to start activity ComponentInfo{CS211D.HW03.PennyConverter/CS211D.HW03.PennyConverter.PennyConverterActivity}: java.lang.NumberFormatException: unable to parse 'null' as integer
02-26 15:26:30.602: E/AndroidRuntime(25020):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-26 15:26:30.602: E/AndroidRuntime(25020):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-26 15:26:30.602: E/AndroidRuntime(25020):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-26 15:26:30.602: E/AndroidRuntime(25020):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-26 15:26:30.602: E/AndroidRuntime(25020):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 15:26:30.602: E/AndroidRuntime(25020):    at android.os.Looper.loop(Looper.java:130)
02-26 15:26:30.602: E/AndroidRuntime(25020):    at android.app.ActivityThread.main(ActivityThread.java:3683)
02-26 15:26:30.602: E/AndroidRuntime(25020):    at java.lang.reflect.Method.invokeNative(Native Method)
02-26 15:26:30.602: E/AndroidRuntime(25020):    at java.lang.reflect.Method.invoke(Method.java:507)
02-26 15:26:30.602: E/AndroidRuntime(25020):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-26 15:26:30.602: E/AndroidRuntime(25020):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-26 15:26:30.602: E/AndroidRuntime(25020):    at dalvik.system.NativeStart.main(Native Method)
02-26 15:26:30.602: E/AndroidRuntime(25020): Caused by: java.lang.NumberFormatException: unable to parse 'null' as integer
02-26 15:26:30.602: E/AndroidRuntime(25020):    at java.lang.Integer.parseInt(Integer.java:356)
02-26 15:26:30.602: E/AndroidRuntime(25020):    at java.lang.Integer.parseInt(Integer.java:332)
02-26 15:26:30.602: E/AndroidRuntime(25020):    at CS211D.HW03.PennyConverter.PennyConverterActivity.onCreate(PennyConverterActivity.java:52)
02-26 15:26:30.602: E/AndroidRuntime(25020):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-26 15:26:30.602: E/AndroidRuntime(25020):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-26 15:26:30.602: E/AndroidRuntime(25020):    ... 11 more
Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
  • 5
    This is a basic java question. Try understanding the method you're using and what it expects and checking what you pass in and ensure that it works. – JoxTraex Feb 27 '12 at 00:23

3 Answers3

2

The problem is that you don't initialize result and when you call Integer.parseInt(result), it fails to parse null. This is apparent from the exception you get:

Caused by: java.lang.NumberFormatException: unable to parse 'null' as integer

Adam Zalcman
  • 26,643
  • 4
  • 71
  • 92
1

The log states clearly that Java is unable to parse "null" as an Integer.

P Varga
  • 19,174
  • 12
  • 70
  • 108
0

like the guys from above said you are parsing null, try initializing your variable before in case your method does not give you a result, probably this result will not be what you want but your code will not crash.

result = 0;

this wont work either: result = "".

Eliud
  • 61
  • 5