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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.