0

In a custom SimpleCursorAdapter, I'm trying to compare a status String, with confusing results.

My string is initialised from the cursor like this (and I've checked with toast that it contains the expected values).

String visitStatus = cursor.getString(cursor.getColumnIndex(CallData.COLUMN_VisitStatus));

visitStatus can be null, Open, Cancelled or Complete.

If I try to compare visitStatus to "any string in quotes", the app crashes with a NullPointerException. Only if I compare to null do I get anything at all - and that is no use to me

if(visitStatus.equals(null)) // the app crashes with a NullPointerException
if(visitStatus == null) // doesn't crash
if(visitStatus != null) // doesn't crash
if(visitStatus == "Complete") // doesn't crash or do anything
if(visitStatus.equals("Complete")) // the app crashes with a NullPointerException.

Basically, I can compare to null, but only in the way that isn't supposed to work. I can't compare to actual strings such as "Open" or "Complete".

I'm going slightly nuts with this, and am badly missing my C# comfort zone. This particular activity is a nightmare of listfragments, contentproviders, customadapters, viewpagers, pagertitlestrips and list row xml templates!

halp!

cherry
  • 517
  • 5
  • 12
  • I see the answers below but they miss that you tried to use .equals which is the correct way to do it. Have you tried doing Log.i("yourapp", visitStatus); to see in LogCat what the contents of String are after you assign it? Perhaps it is not loading the data into that String for some reason, or perhaps there is a problem with the data provider or the way the data is stored (is it s a string?) – logray Dec 13 '12 at 18:28
  • @logray If `visitStatus` is null, then `Log.i("yourapp", visitStatus);` will throw a NPE too... – Sam Dec 13 '12 at 18:35
  • @Sam you are right, was hoping he would make the connection considering the answers below, hence the reason why I said "I see the answers below". But I suppose I had better not suppose: if (visitStatus != null) Log.i("yourapp", visitStatus); – logray Dec 13 '12 at 18:50
  • I used a combination of toast and log.i to diagnose, but couldn't solve it myself. I got as far as being *certain* that my variable visitStatus contained what it was supposed to, but was completely stuck trying to work with it. It's a new day, and I still haven't forgiven Java. – cherry Dec 14 '12 at 09:32
  • This question does no show any research effort! Are you kidding? Who bloody said that? Angryness! – cherry Dec 14 '12 at 09:34
  • I did heaps of research, including - when, out of desperation and time-pressure (more than a day on this one thing!) I got round to asking for help - reading EVERY related question suggested by the "Ask Question" screen after typing up my question and tagging it. Still annoyed at having the question marked down as if I'd been lazy. – cherry Dec 14 '12 at 12:04

2 Answers2

4

This is because visitStatus is null. Whenever you try to access its methods, it crashes. (That is: visitString.equals(), visitString.length(), etc., all will crash.)

However, the equality operator (==) supports null parameters on either side of it. (So, if (null == null) is a valid check.)

You should check like this:

if (visitStatus != null && visitStatus.equals("Complete")) {
    // ...
}

Or, you can do "Yoda syntax" (backwards checking), which supports null parameters:

if ("Complete".equals(visitStatus)) {
    // ...
}

Also, a final note: You cannot compare string contents using == (as in, you cannot do "a" == new String("a"), nor visitString == "Complete"). For a detailed explanation on that, see this Q&A thread.

Community
  • 1
  • 1
Cat
  • 66,919
  • 24
  • 133
  • 141
0

String should be compared using .equals()

The NullPointerException caused because the visitStatus is null

Ajmal Salim
  • 4,142
  • 2
  • 33
  • 41