Search code examples
androidsqliteandroid-cursoradapter

Android - 'cursor.close()' may produce 'java.lang.nullpointerexception'?


I have bellow code which works just fine, but produces a warning:

method invocation 'cursor.close()' may produce 'java.lang.nullpointerexception'

code with warning


Solution

  • cursor_id may not be initialized, as, e.g., your try block checks. You need to add the same validation in the finally block:

    } finally { 
        if (cursor_id != null) {
            cursor_id.close();
        }
    }