Roel Roel -3 years ago 129
Android Question

Adding a contact group gives NullPointerexception on HTC and Samsung devices

I'm trying to add a contacts group with a

. This works on Nexus, Moto G and Asus devices but it crashes on HTC M7, M8, M8 mini and M9, Samsung S4, S5. Because I only have the sources of the normal clean Android I can't find what the Nullpointer is causing by debugging.

None of the values I use in this
are null.
The groups are added to a custom account type specified in authenticator.xml.


public Uri addContactGroup(String title, boolean autoAdd, boolean isReadOnly, boolean isVisible){
ArrayList<ContentProviderOperation> ops = new ArrayList<>();

.withValue(ContactsContract.Groups.TITLE, title)
.withValue(ContactsContract.Groups.ACCOUNT_TYPE, getAccount().type)
.withValue(ContactsContract.Groups.ACCOUNT_NAME, getAccount().name)
.withValue(ContactsContract.Groups.AUTO_ADD, autoAdd)
.withValue(ContactsContract.Groups.GROUP_IS_READ_ONLY, isReadOnly)
.withValue(ContactsContract.Groups.GROUP_VISIBLE, isVisible)
try {
ContentProviderResult[] result = mContext.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
if(result.length > 0) {
return result[0].uri;
} catch (RemoteException | OperationApplicationException e){
Log.e(TAG, e.toString());
return null;

Throws this exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'int
java.lang.Integer.intValue()' on a null object reference
at android.os.Parcel.readException(
at android.database.DatabaseUtils.readExceptionFromParcel(
at android.database.DatabaseUtils.readExceptionWithOperationApplicationExceptionFromParcel(
at android.content.ContentProviderProxy.applyBatch(
at android.content.ContentProviderClient.applyBatch(
at android.content.ContentResolver.applyBatch(
at nl.dalvikvm.stackoverflowquestion.content.proces.CardProcess.addContactGroup(
at nl.dalvikvm.stackoverflowquestion.content.proces.CardProcess.initGroups(
at nl.dalvikvm.stackoverflowquestion.sync.SyncUtils.CreateSyncAccount(
at nl.dalvikvm.stackoverflowquestion.ui.MainActivity$

Answer Source

Finally found the solution!

        .withValue(ContactsContract.Groups.AUTO_ADD, autoAdd ? 1 : 0)
        .withValue(ContactsContract.Groups.GROUP_IS_READ_ONLY, isReadOnly ? 1 : 0)
        .withValue(ContactsContract.Groups.GROUP_VISIBLE, isVisible ? 1 : 0)

Apparently on Samsung and HTC devices boolean values are not accepted as values and they must be converted to integers first. I have no idea why it worked on the other devices.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download