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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex