Mike Baxter
Mike Baxter

Reputation: 7268

Getting null ints from sqlite in android

I have an issue that may in fact be a design issue but I am struggling to find a way around it.

In my sqlite database in my android application, I have a table called Customer. This has around 40 columns of various string and int data types. In practice, any of these columns may or may not be null.

In my code, I have a function simply called getCustomer(), which queries the database for a specific customer, and places all their cells from the database into a Customer class, which contains variables for each column. I can then pass that customer object around as I wish. The getCustomer() function returns this Customer object.

My problem is integers which may be null. I am familiar with how int cannot be null in java, but how Integer can be null. But my problem actually lies in the fact that the cells in the database can be empty (eg null).

For string columns, I simply do this:

Customer cust = new Customer();
cust.firstName = cursor.getString(0);

If cursor.getString(0); returns a null value, then the firstName variable is assigned null. Nice and simple. However with an int:

Customer cust = new Customer();
cust.daysSinceJoining = cursor.getInt(5);

The above crashes at run-time if daysSinceJoining is null. So I tried the following:

Customer cust = new Customer();
if (cursor.getInt(5) != null)
    cust.daysSinceJoining = cursor.getInt(5);

However this gives me a compiler error, as you cannot use an int in a null comparison like that.

How can I get around this problem? How can I retrieve an int from an sqlite database when the int value could be null?

Upvotes: 8

Views: 7200

Answers (5)

Then Enok
Then Enok

Reputation: 661

You can create a cursor wrapper and add new functionality to the cursor class:

public class CustomCursor extends CursorWrapper {

    public CustomCursor(Cursor cursor) { super(cursor); } //simple constructor

    public Integer getInteger(int columnIndex) { // new method to return Integer instead of int
        if (super.isNull(columnIndex)){
            return null;
        }else{
            return super.getInt(columnIndex);
        }
    }
}

Example usage:

Cursor defaultCursor = db.rawQuery("select null,2 ", null);
defaultCursor.moveToFirst();
CustomCursor customCursor = new CustomCursor(defaultCursor);
customCursor.moveToFirst(); //custom cursor can do anything that default cursor can do

int defaultInt0 = defaultCursor.getInt(0); //nulls are usually converted into zero
int defaultInt1 = defaultCursor.getInt(1); //2 is correct

int customInt0 = customCursor.getInt(0); //these 2 are same as above , ie zero and 2
int customInt1 = customCursor.getInt(1);

Integer customInteger0 = customCursor.getInteger(0); // this will give a null Integer
Integer customInteger1 = customCursor.getInteger(1); // this will give a 2 Integer

Log.v("custom log.v call ", "lets see what outputs, null is usually converted to the word 'null' by the String.valueOf method :"
        +String.valueOf(defaultInt0)+","+String.valueOf(defaultInt1)+","
        +String.valueOf(customInt0)+","+String.valueOf(customInt1)+","
        +String.valueOf(customInteger0)+","+String.valueOf(customInteger1)
); 
//V: lets see what outputs, null is usually converted to the word 'null' by the String.valueOf method :0,2,0,2,null,2

Upvotes: 0

Mohammod Hossain
Mohammod Hossain

Reputation: 4114

if (cursor.getInt(5) !=0){

cust.daysSinceJoining = cursor.getInt(5);

}

or

int index = cursor.getColumnIndex(KEY_NAME);
Integer x = null;
if (!cursor.isNull(index)
   cust.daysSinceJoining = cursor.getInt(5);
}

see the documentation getInt() documentation:

Upvotes: -2

nano_nano
nano_nano

Reputation: 12523

Please take a look to that answer:

getInt null-constraints

I think that should work for you:

int lIndex = cursor.getColumnIndexOrThrow(COLUMN);
Integer lInteger = null;
if (!cursor.isNull(lIndex)
lInteger = cursor.getInt(lIndex);

Upvotes: 7

laalto
laalto

Reputation: 152847

@sanders is right about the isNull() method and here's how you edit your code to use it:

Customer cust = new Customer();
if (!cursor.isNull(5))
    cust.daysSinceJoining = cursor.getInt(5);

Upvotes: 16

sanders
sanders

Reputation: 10898

You could try the isNull() function.

Upvotes: 13

Related Questions