java.lang.NumberFormatException: Invalid long: “”

  • Post comments:0 Comments

Whenever we use int or long value we get this type of error Invalid long: “” in android studio logcat.
Here Button and EditText are used for numeric value. Used variable long value. On clicking the button the following logcat error looks.

Process: com.example.myapplication, PID: 10210
    java.lang.NumberFormatException: Invalid long: ""
        at java.lang.Long.invalidLong(Long.java:124)
        at java.lang.Long.parseLong(Long.java:345)
        at com.example.myapplication.MainActivity$1.onClick(MainActivity.java:55)
        at android.view.View.performClick(View.java:4756)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1194)
        at android.view.View$PerformClick.run(View.java:19749)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

java.lang.NumberFormatException: Invalid long: “”

Here the main error is NumberFormatException.
When convent one data to another data type, use try catch block.
Note: recommended use exception try…catch block to prevent crash app.

And this type of error also comes when the field is empty or unsigned value. So use validation for prevention.
Sometimes this error comes due to invalid data type convention.
Same error here, so if you want to use only digit value then use valid validation.
If you want to use both strings and digits, use convention and validation.
See the following program used validation but unfortunately crashed the application.

if (TextUtils.isEmpty(f)||TextUtils.isEmpty(s)){
fino.setError(“empty”);
sino.setError(“is empty”);
}
In the above program blank filled validation is used, still get the same error.
Never mind, the solution is easily given here.
Now check the following verification.

if (TextUtils.isEmpty(f)||TextUtils.isEmpty(s)){
fno.setError(“empty”);
sno.setError(“is empty”);
return;
}
error is solve
When an empty EditText is detected, the error message is returned without crashing the app.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments