How to solve java.lang.NumberFormatException in Android
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simplecal/com.example.simplecal.MainActivity}: java.lang.NumberFormatException: Invalid float: “”
a = Float.valueOf(edt1.getText().toString());
b = Float.valueOf(edt2.getText().toString());
Code
EditText edt1 = (EditText) findViewById(R.id.display1);
a = Float.valueOf(edt1.getText().toString());
EditText edt2 = (EditText)findViewById(R.id.display2);
b = Float.valueOf(edt2.getText().toString());
calculate.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
result=num1+num2;
}
});
Stack Trace Error
02-15 09:05:24.187: D/AndroidRuntime(1129): Shutting down VM
02-15 09:05:24.197: W/dalvikvm(1129): threadid=1: thread exiting with uncaught exception (group=0xb3ac2b90)
02-15 09:05:24.257: E/AndroidRuntime(1129): FATAL EXCEPTION: main
02-15 09:05:24.257: E/AndroidRuntime(1129): Process: com.example.simplecal, PID: 1129
02-15 09:05:24.257: E/AndroidRuntime(1129): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simplecal/com.example.simplecal.MainActivity}: java.lang.NumberFormatException: Invalid float: “”
02-15 09:05:24.257: E/AndroidRuntime(1129): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
02-15 09:05:24.257: E/AndroidRuntime(1129): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
02-15 09:05:24.257: E/AndroidRuntime(1129): at android.app.ActivityThread.access$700(ActivityThread.java:135)
02-15 09:05:24.257: E/AndroidRuntime(1129): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
02-15 09:05:24.257: E/AndroidRuntime(1129): at android.os.Handler.dispatchMessage(Handler.java:102)
02-15 09:05:24.257: E/AndroidRuntime(1129): at android.os.Looper.loop(Looper.java:137)
02-15 09:05:24.257: E/AndroidRuntime(1129): at android.app.ActivityThread.main(ActivityThread.java:4998)
02-15 09:05:24.257: E/AndroidRuntime(1129): at java.lang.reflect.Method.invokeNative(Native Method)
02-15 09:05:24.257: E/AndroidRuntime(1129): at java.lang.reflect.Method.invoke(Method.java:515)
02-15 09:05:24.257: E/AndroidRuntime(1129): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-15 09:05:24.257: E/AndroidRuntime(1129): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-15 09:05:24.257: E/AndroidRuntime(1129): at dalvik.system.NativeStart.main(Native Method)
02-15 09:05:24.257: E/AndroidRuntime(1129): Caused by: java.lang.NumberFormatException: Invalid float: “”
02-15 09:05:24.257: E/AndroidRuntime(1129): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
02-15 09:05:24.257: E/AndroidRuntime(1129): at java.lang.StringToReal.parseFloat(StringToReal.java:289)
02-15 09:05:24.257: E/AndroidRuntime(1129): at java.lang.Float.parseFloat(Float.java:300)
02-15 09:05:24.257: E/AndroidRuntime(1129): at java.lang.Float.valueOf(Float.java:337)
02-15 09:05:24.257: E/AndroidRuntime(1129): at com.example.simplecal.MainActivity.onCreate(MainActivity.java:45)
02-15 09:05:24.257: E/AndroidRuntime(1129): at android.app.Activity.performCreate(Activity.java:5243)
02-15 09:05:24.257: E/AndroidRuntime(1129): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-15 09:05:24.257: E/AndroidRuntime(1129): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
02-15 09:05:24.257: E/AndroidRuntime(1129): … 11 more
Solution
a = Float.valueOf(edt1.getText().toString());
b = Float.valueOf(edt2.getText().toString());
Those two are in into the onClick() method
It will be like this
public void onClick(View v)
{
num1 = Float.valueOf(edt1.getText().toString());
num2 = Float.valueOf(edt2.getText().toString());
result=num1+num2;
}
Nice Post