0

Hello stackoverflow world! There comes a time when the answer you're looking for is not already here and so you'll have to ask.

My issue is I'm checking for null values using an if statement and I'm still getting a NullPointerException.

I'm having my app tested by someone and they run into a NullPointerException but I'm not able to recreate the bug for the life of me. making it harder to pinpoint the issue.

This is the CrashReport on dev console for the app:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dev.admin.blacklist/com.dev.admin.blacklist.Page_RecentCalls}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2391)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
    at android.app.ActivityThread.access$800(ActivityThread.java:166)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1357)
    at android.os.Handler.dispatchMessage(Handler.java:110)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:5387)
    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:831)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:647)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
    at com.dev.admin.blacklist.Page_RecentCalls.getRecentContacts(Page_RecentCalls.java:110)
    at com.dev.admin.blacklist.Page_RecentCalls.onCreate(Page_RecentCalls.java:32)
    at android.app.Activity.performCreate(Activity.java:5363)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2335)
    ... 11 more

and this is the line 110 that is throwing this error:

    Cursor cursor = getContentResolver().query(queryUri, projection, null, null, sortOrder);


    while (cursor.moveToNext())
    {
        String phoneNumber =  cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
        String title = (cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME)));
        if(phoneNumber != null && !phoneNumber.isEmpty() && !phoneNumber.equals(manager.getVoiceMailNumber().toString()))
        {
            if(title == null || title.isEmpty())title = "No-name";
            if(recentcontacts.size() > 1 )
            {
                String one = ""; 
                one = recentcontacts.get(recentcontacts.size() - 1).getFirst(); //Line 110 error was here
                if(!one.equals(title)) recentcontacts.add(new Object_TwoString(title,phoneNumber));
            }
            else  recentcontacts.add(new Object_TwoString(title,phoneNumber));
        }

    }
    cursor.close();
    return recentcontacts;
}
}




public Object_TwoString(String one, String two)
{
    setStrings(one,two);
}

public String[] getStrings(){ String[] strings = {firstString,secondString}; return strings;}
public String getFirst(){return firstString;}
public String getSecond(){return secondString;}

public Boolean IsContained(List<Object_TwoString> obj)
{
    for(Object_TwoString  ts: obj)
    {
        if(ts.getFirst().equals(firstString) && ts.getSecond().equals(secondString))return true;
    }
    return false;
}
Ash E
  • 3
  • 4
  • Manager.getVoicemailNumber() might be null so .toString() would throw NPE? – Jan Nov 11 '15 at 21:33
  • hmm...I'll have to try checking for a null voicemail number, but if that were the case wouldn't the error be thrown on the line where I'm checking for voicemail number? (line 105) instead of line 110? – Ash E Nov 11 '15 at 21:38

1 Answers1

-1

The actual NullPointerException is happening here:

 getContentResolver()

Calling that method returns null and then calling query() on that fails because you can't call methods on null objects. (FWIW: null shouldn't exist and is a cancer on software engineering projects everywhere)

I strongly suspect some kind of initialization step necessary for getContentResolver() (which is regrettably not enforced by the types) is biting you here. There is a temporal coupling requiring you to do some kind of rain dance to make that method work correctly which is a bad design choice.

Alain O'Dea
  • 21,033
  • 1
  • 58
  • 84
  • @AshE good point. I've updated in case the general NPE solution this is marked as a duplicate for this doesn't help you. – Alain O'Dea Nov 11 '15 at 22:05
  • Thanks!...I should have seen this. yes..It was the getContentResolver() that was giving a null value for voicemailnumber (no sim-card in) – Ash E Nov 11 '15 at 22:49