MrTimotheos
MrTimotheos

Reputation: 937

Why is my null check against my string not working?

Alright here we go.

I am probably asking the most popular question on here and I'm freaking seasoned dev.

Basically I am comparing if a String is null. IT IS NULL But sadly, it is for some reason bypassing my if and going straight for my else.

I feel ashamed and guilty for asking this oh so simple question but I am at a loss.

What am I missing?

Relevant Code:

  Log.i(TAG,"mBeforeId..." + mBeforeId);
            if(mBeforeId == null){
                Log.i(TAG,"mBeforeId INSIDE IF..." + mBeforeId);
                 Toast.makeText(getActivity(),"These are no current listings before these.",Toast.LENGTH_SHORT).show();
            }else{
                getSubReddit(mSubReddit, Constants.HOT_CATEGORY, mBeforeId, null);
            }

LogCat:

 12-31 13:34:04.139  10383-10383/com.android.finalproject.tubbsreddit I/SubRedditFragment﹕ mBeforeId...null

As you can clearly see, It is saying mBeforeId is null but it refuses to go to my if and makes pals with my else.

GAR.

Upvotes: 0

Views: 69

Answers (2)

Prem
Prem

Reputation: 4831

In Android Log statements, if you try to print a String which has null value, it throws an exception. This might be the reason why you are seeing this unexpected behavior.

Try to remove the log cat statements if the value is null. It might work. Give a try

Upvotes: 1

MrTimotheos
MrTimotheos

Reputation: 937

Fixed it.

Basically

mBeforeId was "null", not null. It was the actual string "null".

Don't you just love programming?

Upvotes: 1

Related Questions