Fix NPE with use of getFirstRowXxx

Make sure not to cast null into a primitive type.
(i.e. If null isn't expected as a return value, make sure to set a non-null
default value.)

Bug 3032143

Change-Id: I9a344d765c75a66f529ad8d99b00b2b919139f9c
This commit is contained in:
Makoto Onuki 2010-09-24 14:50:20 -07:00
parent 1f61263136
commit d25d87c7ba
2 changed files with 17 additions and 11 deletions

View File

@ -1455,17 +1455,18 @@ public abstract class EmailContent {
*/
public static boolean isValidId(Context context, long accountId) {
return null != Utility.getFirstRowLong(context, CONTENT_URI, ID_PROJECTION,
ID_SELECTION, new String[] {Long.toString(accountId)}, null, 0);
ID_SELECTION, new String[] {Long.toString(accountId)}, null,
ID_PROJECTION_COLUMN);
}
/**
* Check a single account for security hold status.
*/
public static boolean isSecurityHold(Context context, long accountId) {
Long flags = Utility.getFirstRowLong(context,
return (Utility.getFirstRowLong(context,
ContentUris.withAppendedId(Account.CONTENT_URI, accountId),
ACCOUNT_FLAGS_PROJECTION, null, null, null, ACCOUNT_FLAGS_COLUMN_FLAGS);
return (flags != null) && ((flags & Account.FLAGS_SECURITY_HOLD) != 0);
ACCOUNT_FLAGS_PROJECTION, null, null, null, ACCOUNT_FLAGS_COLUMN_FLAGS, 0L)
& Account.FLAGS_SECURITY_HOLD) != 0;
}
/**
@ -1474,7 +1475,7 @@ public abstract class EmailContent {
public static long getInboxId(Context context, long accountId) {
return Utility.getFirstRowLong(context, Mailbox.CONTENT_URI, ID_PROJECTION,
FIND_INBOX_SELECTION, new String[] {Long.toString(accountId)}, null,
ID_PROJECTION_COLUMN);
ID_PROJECTION_COLUMN, -1L);
}
/**
@ -2294,19 +2295,17 @@ public abstract class EmailContent {
}
public static int getUnreadCountByMailboxType(Context context, int type) {
return Utility.getFirstRowLong(context, Mailbox.CONTENT_URI,
return Utility.getFirstRowInt(context, Mailbox.CONTENT_URI,
MAILBOX_SUM_OF_UNREAD_COUNT_PROJECTION,
MAILBOX_TYPE_SELECTION,
new String[] { String.valueOf(type) }, null, UNREAD_COUNT_COUNT_COLUMN)
.intValue();
new String[] { String.valueOf(type) }, null, UNREAD_COUNT_COUNT_COLUMN, 0);
}
public static int getMessageCountByMailboxType(Context context, int type) {
return Utility.getFirstRowLong(context, Mailbox.CONTENT_URI,
return Utility.getFirstRowInt(context, Mailbox.CONTENT_URI,
MAILBOX_SUM_OF_MESSAGE_COUNT_PROJECTION,
MAILBOX_TYPE_SELECTION,
new String[] { String.valueOf(type) }, null, MESSAGE_COUNT_COUNT_COLUMN)
.intValue();
new String[] { String.valueOf(type) }, null, MESSAGE_COUNT_COUNT_COLUMN, 0);
}
/**

View File

@ -1974,6 +1974,10 @@ public class ProviderTests extends ProviderTestCase2<EmailProvider> {
assertEquals(1, getMessageCount(b2.mId));
assertEquals(2, getMessageCount(b3.mId));
assertEquals(1, getMessageCount(b4.mId));
// No such mailbox type.
assertEquals(0, Mailbox.getMessageCountByMailboxType(c, 99999));
assertEquals(0, Mailbox.getUnreadCountByMailboxType(c, 99999));
}
private static Message createMessage(Context c, Mailbox b, boolean starred, boolean read) {
@ -2040,6 +2044,9 @@ public class ProviderTests extends ProviderTestCase2<EmailProvider> {
Mailbox b2i = ProviderTestUtils.setupMailbox("b2b", a2.mId, true, c, Mailbox.TYPE_INBOX);
assertEquals(b2i.mId, Account.getInboxId(c, a2.mId));
// No account found.
assertEquals(-1, Account.getInboxId(c, 999999));
}
public void testGetMailboxType() {