• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Why am I getting NumberFormatException?

I'm fairly new to Android and am confused why this doesn't work. Why am I getting a number format problem?

LogCat:
Code:
03-27 17:14:01.598: ERROR/AndroidRuntime(26827): java.lang.NumberFormatException: 
03-27 17:14:01.598: ERROR/AndroidRuntime(26827):     at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:267)
03-27 17:14:01.598: ERROR/AndroidRuntime(26827):     at java.lang.Double.parseDouble(Double.java:287)
03-27 17:14:01.598: ERROR/AndroidRuntime(26827):     at com.prattia.webs.cheaterphysics.Pythag$1.onClick(Pythag.java:51)
03-27 17:14:01.598: ERROR/AndroidRuntime(26827):     at android.view.View.performClick(View.java:2465)
03-27 17:14:01.598: ERROR/AndroidRuntime(26827):     at android.view.View$PerformClick.run(View.java:8907)
03-27 17:14:01.598: ERROR/AndroidRuntime(26827):     at android.os.Handler.handleCallback(Handler.java:587)
03-27 17:14:01.598: ERROR/AndroidRuntime(26827):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-27 17:14:01.598: ERROR/AndroidRuntime(26827):     at android.os.Looper.loop(Looper.java:123)
03-27 17:14:01.598: ERROR/AndroidRuntime(26827):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-27 17:14:01.598: ERROR/AndroidRuntime(26827):     at java.lang.reflect.Method.invokeNative(Native Method)
03-27 17:14:01.598: ERROR/AndroidRuntime(26827):     at java.lang.reflect.Method.invoke(Method.java:521)
03-27 17:14:01.598: ERROR/AndroidRuntime(26827):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
03-27 17:14:01.598: ERROR/AndroidRuntime(26827):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-27 17:14:01.598: ERROR/AndroidRuntime(26827):     at dalvik.system.NativeStart.main(Native Method)

And the code with line 51 noted:
Code:
public class Pythag extends Activity {

	Button submit, clear;
	EditText at, bt, ct;
	TextView error, afinal, bfinal, cfinal;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pythag);
		
					
		submit = (Button) findViewById(R.id.bSubmitP);
		clear = (Button) findViewById(R.id.bClearP);
		at = (EditText) findViewById(R.id.etSideAP);
		bt = (EditText) findViewById(R.id.etSideBP);
		ct = (EditText) findViewById(R.id.etSideCP);
		error = (TextView) findViewById(R.id.tvErrorP);
		afinal = (TextView) findViewById(R.id.tvSideAP);
		bfinal = (TextView) findViewById(R.id.tvSideBP);
		cfinal = (TextView) findViewById(R.id.tvSideCP);
		
		submit.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				double ad, bd, cd;
				
				error.setText("");
					
				Doublify(at);
				Doublify(bt);
				Doublify(ct);
				
				ad = Double.parseDouble(at.getText().toString());
				bd = Double.parseDouble(bt.getText().toString());
				cd = Double.parseDouble(ct.getText().toString()); //Line 51. When I ran the program, I entered values for a and b but left c blank
				
				if(ad<0 || bd<0 || cd<0) {
					error.setText("Sides must be positive");
				} else {
					if(ad == 0 && bd!=0 && cd!=0)
						ad = Math.sqrt(cd*cd-bd*bd);
					else if(bd == 0 && ad!=0 && cd!=0)
						bd = Math.sqrt(cd*cd-ad*ad);
					else if(cd == 0 && ad!=0 & bd!=0)
						cd = Math.sqrt(ad*ad+bd*bd);
					else
						error.setText("Must enter two sides");
					ad = round(ad);
					bd = round(bd);
					cd = round(cd);
					at.setText(Double.toString(ad));
					bt.setText(Double.toString(bd));
					ct.setText(Double.toString(cd));	
				}
			}		
		});
		
	private double Doublify(EditText editText){
	     try {
	       return Double.parseDouble(editText.getText().toString());
	     } catch (NumberFormatException e) {
	        return 0;
	     }
	 }
}
 
I'm guessing it doesnt like converting an empty string into a double.

ct.getText().toString() is probably an empty string but not null...
 
Alp is correct. You need to verify that what is in the text field is, in fact a number before trying to parse it.
 
Back
Top Bottom