Merge "Fix potential NPE with null cursor" into klp-dev
diff --git a/src/com/android/contacts/common/list/DirectoryListLoader.java b/src/com/android/contacts/common/list/DirectoryListLoader.java
index be9a8e9..dedd62e 100644
--- a/src/com/android/contacts/common/list/DirectoryListLoader.java
+++ b/src/com/android/contacts/common/list/DirectoryListLoader.java
@@ -147,6 +147,9 @@
 
         Cursor cursor = context.getContentResolver().query(DirectoryQuery.URI,
                 DirectoryQuery.PROJECTION, selection, null, DirectoryQuery.ORDER_BY);
+        if (cursor == null) {
+            return result;
+        }
         try {
             while(cursor.moveToNext()) {
                 long directoryId = cursor.getLong(DirectoryQuery.ID);
diff --git a/src/com/android/contacts/common/list/ProfileAndContactsLoader.java b/src/com/android/contacts/common/list/ProfileAndContactsLoader.java
index 9d2bbbb..e31b8ec 100644
--- a/src/com/android/contacts/common/list/ProfileAndContactsLoader.java
+++ b/src/com/android/contacts/common/list/ProfileAndContactsLoader.java
@@ -56,6 +56,8 @@
         if (mLoadProfile) {
             cursors.add(loadProfile());
         }
+        // ContactsCursor.loadInBackground() can return null; MergeCursor
+        // correctly handles null cursors.
         final Cursor contactsCursor = super.loadInBackground();
         cursors.add(contactsCursor);
         return new MergeCursor(cursors.toArray(new Cursor[cursors.size()])) {
@@ -68,11 +70,17 @@
     }
 
     /**
-     * Loads the profile into a MatrixCursor.
+     * Loads the profile into a MatrixCursor. On failure returns null, which
+     * matches the behavior of CursorLoader.loadInBackground().
+     *
+     * @return MatrixCursor containing profile or null on query failure.
      */
     private MatrixCursor loadProfile() {
         Cursor cursor = getContext().getContentResolver().query(Profile.CONTENT_URI, mProjection,
                 null, null, null);
+        if (cursor == null) {
+            return null;
+        }
         try {
             MatrixCursor matrix = new MatrixCursor(mProjection);
             Object[] row = new Object[mProjection.length];
diff --git a/src/com/android/contacts/common/model/RawContactModifier.java b/src/com/android/contacts/common/model/RawContactModifier.java
index 0ba029a..954123b 100644
--- a/src/com/android/contacts/common/model/RawContactModifier.java
+++ b/src/com/android/contacts/common/model/RawContactModifier.java
@@ -305,7 +305,7 @@
     public static EditType getBestValidType(RawContactDelta state, DataKind kind,
             boolean includeSecondary, int exactValue) {
         // Shortcut when no types
-        if (kind.typeColumn == null) return null;
+        if (kind == null || kind.typeColumn == null) return null;
 
         // Find type counts and valid primary types, bail if none
         final SparseIntArray typeCount = getTypeFrequencies(state, kind);
@@ -347,6 +347,8 @@
      * {@link #getBestValidType(RawContactDelta, DataKind, boolean, int)}.
      */
     public static ValuesDelta insertChild(RawContactDelta state, DataKind kind) {
+        // Bail early if invalid kind
+        if (kind == null) return null;
         // First try finding a valid primary
         EditType bestType = getBestValidType(state, kind, false, Integer.MIN_VALUE);
         if (bestType == null) {