Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking null and "emptyness" in Java (Android) --> crash

Tags:

java

android

I made an if/else statement that checks if the value of an Edit Text (the value of the Edit Text is saved in myPrefs) is empty. If it is empty, there will show up an error. If it is not empty, another dialog will show op and the user can go further in the app. So that is what I want.

The problem is, when I'm checking whether it is empty or not, the app crashes. This is my code, I already implemented the || (with null and isEqual(""), but that didn't work out.

if (myPrefs.getString("TELHUISARTS", null).equals("") || myPrefs.getString("TELHUISARTS",null).equals(null)) {
    String contactWithQuotes = getResources().getString(R.string.Contactwithquotes);

    AlertDialog.Builder alertDialogBuilder2 = new AlertDialog.Builder(Gevaar.this);

    // set title
    alertDialogBuilder2.setTitle("No phone number found");

    // set dialog message
    alertDialogBuilder2
        .setMessage("You have not entered a phone number for your GP. You can do this at " +contactWithQuotes+ ".")
        .setCancelable(false)
        .setNegativeButton("OK",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                // if this button is clicked, just close
                // the dialog box and do nothing
                dialog.cancel();
            }
        });

    AlertDialog alertDialog2 = alertDialogBuilder2.create();

    // show it
    alertDialog2.show();
} else {
    // create alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();

    // show it
    alertDialog.show();

}

The error:

05-01 23:17:09.295  24300-24300/com.company.myapp 
E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.company.myapp, PID: 24300
    java.lang.NullPointerException
            at com.company.myapp.Gevaar$11.onClick(Gevaar.java:609)
            at android.view.View.performClick(View.java:4456)
            at android.view.View$PerformClick.run(View.java:18465)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            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:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
like image 471
Stylo Avatar asked Sep 06 '26 14:09

Stylo


2 Answers

You probably want to change your condition from:

 if (myPrefs.getString("TELHUISARTS", null).equals("") || myPrefs.getString("TELHUISARTS",null).equals(null))

To:

 if (myPrefs.getString("TELHUISARTS",null) == null || myPrefs.getString("TELHUISARTS", null).equals(""))

The way you did it, if myPrefs.getString("TELHUISARTS",null) is indeed null, you will get a NullPointerException in the first check, since it would be equivalent to calling null.equals(""). Also, you should use == instead of equals() to check if a reference is null.

like image 199
Anderson Vieira Avatar answered Sep 08 '26 05:09

Anderson Vieira


Instead of myPrefs.getString("TELHUISARTS", null).equals("")

do

"".equals(myPrefs.getString("TELHUISARTS", null))

because "".equals(null) return false and null.equals("") crash, because did on a null object

like image 40
florent champigny Avatar answered Sep 08 '26 04:09

florent champigny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!