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;
}