Search code examples
androiddatabasecursorandroid-sqlite

cursor.getLong(1) not throwing Exception


I have an field in database that is null When I retrieve it in a cursor it is null. I can tell that from the

cursor.isNull(1) is true So when I do cursor.getLong(1) it should throw an exception according to the documentation. but it actually retrieves 0 without and exception. any ideas why?


Solution

  • If you take a look at the implementation of the method getLong() in MatrixCursor , you can see the following code:

        @Override
    public long getLong(int column) {
        Object value = get(column);
        if (value == null) return 0;
        if (value instanceof Number) return ((Number) value).longValue();
        return Long.parseLong(value.toString());
    }
    

    If the value is null, then 0 is returned.