java.lang.NumberFormatException: Invalid int: "" : Error

Farrukh Faizy picture Farrukh Faizy · Jan 22, 2016 · Viewed 26.7k times · Source

I am doing some calculation but unable to parse a string into int or even in float.I searched for solution and i read it somewhere there must be a empty string but i checked my editText using

log.v("Valuee",e1.getText().toString());

and its print the values prove that string is not empty.. What i am missing ?

Here is logcat

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.farrukh.bmi/com.example.farrukh.bmi.Main2Activity}: java.lang.NumberFormatException: Invalid int: "" at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NumberFormatException: Invalid int: "" at java.lang.Integer.invalidInt(Integer.java:137) at java.lang.Integer.parseInt(Integer.java:358) at java.lang.Integer.parseInt(Integer.java:331) at com.example.farrukh.bmi.Main2Activity.onCreate(Main2Activity.java:31) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)  at android.app.ActivityThread.access$800(ActivityThread.java:135)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:136)  at android.app.ActivityThread.main(ActivityThread.java:5017)  at java.lang.reflect.Method.invokeNative(Native Method) 

Here is MainActivity.java

 public class Main2Activity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);

          final   Button b = (Button) findViewById(R.id.button);
          final   EditText e1 = (EditText) findViewById(R.id.editText2);
          final   TextView text = (TextView) findViewById(R.id.textView4);
          final  String height = e1.getText().toString();


          final int a = Integer.parseInt(height); //got error while parsing

     b.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                 //   Log.v("EditText",e1.getText().toString());

                }
            });


        }


    }

Here is activity.xml

<Button
        android:layout_width="132dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
         android:layout_gravity="center_horizontal"
        android:text="CLICK"
        android:clickable="true"
        android:id="@+id/button"/>

<EditText
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/editText2"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:gravity="center" />


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/textView4"
        android:layout_gravity="center_horizontal"
        android:gravity="center"
        android:layout_marginTop="20dp"
        android:textColor="#3b9ff0" />

Answer

Stultuske picture Stultuske · Jan 22, 2016

You are trying to parse an empty String ( "" ) to a numerical value, but "" is not a numerical value.

Make sure you set it to required, or check for emptiness before trying to parse it.

final int a = !height.equals("")?Integer.parseInt(height) : 0;

for instance.

EDIT:

If you have added spaces, so it would be " ";

height = height.trim();
final int a = !height.equals("")?Integer.parseInt(height) : 0;

should do the trick.