Skip to main content

Android App crashes at line 54(Integer.parseInt) and not entirely sure why



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




Comments

  1. 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

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

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 1 Final Exam 2011 latest (hot hot hot)

  Hi! I have been posted content of ccna1 final exam (latest and only question.) I will post the answer and insert image on sunday. If you care, please subscribe your email an become a first person have full test content. Subcribe now  Some question  have not content because this question have images content. So that can you wait for me? SUNDAY 1. A user sees the command prompt: Router(config-if)# . What task can be performed at this mode? Reload the device. Perform basic tests. Configure individual interfaces. Configure individual terminal lines. 2. Refer to the exhibit. Host A attempts to establish a TCP/IP session with host C. During this attempt, a frame was captured with the source MAC address 0050.7320.D632 and the destination MAC address 0030.8517.44C4. The packet inside the captured frame has an IP source address 192.168.7.5, and the destination IP address is 192.168.219.24. At which point in the network was this packet captured? leaving host A leaving ATL leaving...