java.lang.NumberFormatException: Invalid long: “”

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(
        at java.lang.Long.parseLong(
        at com.example.myapplication.MainActivity$1.onClick(
        at android.view.View.performClick(
        at android.view.View$
        at android.os.Handler.handleCallback(
        at android.os.Handler.dispatchMessage(
        at android.os.Looper.loop(
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(

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)){
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)){
sno.setError(“is empty”);
error is solve
When an empty EditText is detected, the error message is returned without crashing the app.

Leave a Reply