1

A lot of my users are facing a NullPointerException issue while instantiating content descriptor.

Fatal Exception: java.lang.NullPointerException: Attempt to read from field 'java.lang.String android.content.UriMatcher.mText' on a null object reference
       at android.content.UriMatcher.addURI(UriMatcher.java:186)
       at com.getsuperapp.chat.db.ContentDescriptor.getUriMatcher(SourceFile:25)
       at com.getsuperapp.chat.db.DatabaseProvider.query(SourceFile:33)
       at android.content.ContentProvider.query(ContentProvider.java:1017)
       at android.content.ContentProvider$Transport.query(ContentProvider.java:238)
       at android.content.ContentResolver.query(ContentResolver.java:497)
       at android.content.ContentResolver.query(ContentResolver.java:439)

The set of devices and environment can be found here.

The ContentDescriptor file is

/**
* A  few constants from other classes used in the file
*
* from UserTable.java
* public static final String PATH = "user_table";
* public static final int PATH_TOKEN = 10;
*/
public class ContentDescriptor {

    private static UriMatcher URI_MATCHER = null;
    private static Uri BASE_URI = null;

    public static UriMatcher getUriMatcher(Context appContext) {

        String AUTHORITY = appContext.getPackageName() + ".quickblox";

        if (URI_MATCHER == null) {
            URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
//The line below is throwing the exception.
            URI_MATCHER.addURI(AUTHORITY, UserTable.PATH, UserTable.PATH_TOKEN); 
            URI_MATCHER.addURI(AUTHORITY, DialogTable.PATH, DialogTable.PATH_TOKEN);
            URI_MATCHER.addURI(AUTHORITY, MessageTable.PATH, MessageTable.PATH_TOKEN);
            URI_MATCHER.addURI(AUTHORITY, ChatInfoTable.PATH, ChatInfoTable.PATH_TOKEN);
        }
        return URI_MATCHER;
    }

    public static Uri getBaseUri(Context appContext) {
        String AUTHORITY = appContext.getPackageName() + ".quickblox";
        if (BASE_URI == null) {
            BASE_URI = Uri.parse("content://" + AUTHORITY);
        }
        return BASE_URI;
    }

}

Let me know if you need any further information. Can anyone provide me some visibility into this. I'm unable to reproduce the crash at my end.

EDIT

Guys stop brushing this into any other NPE category without reading. Its running on a lot of devices without trouble. The initialisation parameters are constants. The line on which its crashing is commented in the code too.

humblerookie
  • 4,717
  • 4
  • 25
  • 40
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Jens Aug 01 '16 at 05:35
  • Thanks but it isn't really. The issue is buried within aosp's content descriptor. I'd like some insight into why this really happens. if you would have read the code, the instantiation is passed constants so the problem isn't really with the inputs – humblerookie Aug 01 '16 at 05:37
  • One of the static fields(PATH or PATH_TOKEN) in your model classes are coming null. Make sure they aren't modified anywhere. Also, you may want to use getters/setters for those fields. – Shaishav Aug 01 '16 at 05:38
  • Again, Like I have marked the path and pathtoken all are constants. check the comment above the file. I am running this on a lot of devices and its not crashing. Hence needed some input. – humblerookie Aug 01 '16 at 05:39
  • Its highlighted via comment. – humblerookie Aug 01 '16 at 05:49

1 Answers1

1

What are the values of your AUTHORITY, PATHs and PATH_TOKENs (all of them)? Depending on the values and the Android version the app is running on your app might fall into the bug fixed in UriMatcher. I guess this is the place you want to start digging into. Also, did you receive the bug report from the developer console or some king of bug reporting library? If yes, what Android version have the reports in common? It might help to reproduce the failure. Or get a ton of devices/emulators running on different versions and try to get it manually.

GPuschka
  • 522
  • 3
  • 9
  • Here is the link to all my path tokens. http://pastie.org/private/f3pzxsjh331kgiciaa2vw I use Fabric for my crash logging and the crash happens on quite varied devices really recently it has happened on a Nexus 5 too http://crashes.to/s/3297e6797ca. I had raised an issue with google for this but it has not yet been even been assigned. https://code.google.com/p/android/issues/detail?id=216791 – humblerookie Aug 04 '16 at 08:41
  • Unfortunately there isn't anything common.It's happening on varied devices, on android versions ranging from lollipop to marshmallow(5.0.1 to 6.0.1). A shit load of thanks for the link, helps me get started :) – humblerookie Aug 04 '16 at 08:43
  • I don't think that fix would work for me since I don't have '\' character in my path – humblerookie Aug 04 '16 at 08:46
  • 1
    Yes, I just ran through the implementation with you input data and there should be no way that it crashes. Which leaved on last question: is the class called from different processes or threads? If yes, then you may want to try to synchronize the access to getUriMatcher and see if that helps. But I guess the main problem is that you cannot reproduce the error and therefore not test the fix against it. On the other hand, synchronizing the access does not break anything so try right away. – GPuschka Aug 04 '16 at 10:37