• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Check for null not working

Greum

Well-Known Member
I have the following method.

Java:
    public void delete(String name, Context x) {
        String msg;
        // Check if anything input
        if (name == null) {
            msg = "No input";
        } else {
            // Count the number of rows
            String query = "SELECT COUNT(*) FROM " + TABLE_NA +
                    " WHERE " +
                    NA_NAME + " = '" + name + "';";
            Cursor c = mDB.rawQuery(query, null);
            int count = 0;
            if (null != c)
                if (c.getCount() > 0) {
                    c.moveToFirst();
                    count = c.getInt(0);
                }
            c.close();
            // Now run the delete
            query = "DELETE FROM " + TABLE_NA +
                    " WHERE " +
                    NA_NAME + " = '" + name + "';";

            Log.i("delete() = ", query);
            mDB.execSQL(query);
            msg = count + " record(s) deleted";
        }
        makeToast(msg, x);
    }

The delete process works fine but I have added the check at the beginning to see if the input is empty, and it's not working. If I try to delete a row with empty input, the Toast message is "0 record(s) deleted" rather than "No input".

The log is reporting

I/delete() =: DELETE FROM names_and_addresses WHERE name = '';

From the delete point of view the result is the same, but I plan to put a similar check in the input method as I don't want my db table filled will null records.

Can anyone see where I'm going wrong?
 
I've done it again, found the answer myself.

TextUtils.isEmpty(name)

Though I'd like to know why name == null doesn't work if anyone can enlighten me...
 
I've done it again, found the answer myself.

TextUtils.isEmpty(name)

Though I'd like to know why name == null doesn't work if anyone can enlighten me...

I don't really know WHY but it seems to be a quirk of Java (at least if you come from another language background). Strings need ,isEqual

I think it's related to the type. From what I've seen, it looks like you can only use == with primitive types. Maybe someone with more experience can add to that (or correct it)
 
I just checked with my son, who is studying Java in college, and he confirms that what I said is accurate. == only works with primitives. For objects, use .equals()
 
I've done it again, found the answer myself.

TextUtils.isEmpty(name)

Though I'd like to know why name == null doesn't work if anyone can enlighten me...
Under the hood, every Java object (like your String object called "name") is actually a pointer to a memory structure that is the object. So think of "name" not so much as an object as a pointer to the location of the object in memory. When you ask if "name" is null, what you are actually asking is if the pointer (name) is pointing at address 00000000. Objects are never stored at memory address 00000000, so that address is given the designation of "null".

However, when the first parameter to your "delete" method is an empty string, "name" is actually pointing at a real object of type "String" that happens to have zero characters in it. So the test for (name==null) fails.

There may be times when "==" may be used in Java, but they would be really rare. If "name1" and "name2" are both Strings, the check for "name1==name2" is asking if the two pointers, name1 and name2, are pointing to the very same memory address where the object is found. In this case there is only one actual object, but two pointers pointing at it. On the other hand, the check for name1.equals(name2) will return true if the objects pointed to by name1 and name2 have the same characters, even if they are separate objects located at different memory addresses. Similarly, name.isEmpty() returns true if name is a string with zero characters in it. You may wonder how much memory is occupied by a String with zero characters in it. Actually quite a few. Every Java object has a member that describes what kind of an object it is (String in this case) and in the case of Strings, another member is the count of the number of characters in the string. This takes up memory even if the String is empty.
 
@RLScott - thanks for the detailed explanation! It looks like about what I was saying, but with a LOT more detail on why it works that way. It's always good to understand why things are the way they are.
 
Back
Top Bottom