There is a database with columns (for example) id | st1 | St2, write to St2, for example, the number 4, then we get the record 1 | null | 4. There is a TextView in the activity in which the last value of the column st1 is written by code, during the start of the activity we refer to the database and make a request only for st1, but the cursor accesses the null value and crashes the execution, how to perform the check? if there were no records, it is checked simply through

if (cursor.moveToFirst) { textview.setText(получаем текст из БД); } else { textview.setText("рандом текст"); } 

but how to be when in this desired cell null can not understand

  • 2
    if (cursor.moveToFirst! = null)? - J. Meirlen Nov.
  • There are different ways to solve the problem. You can filter in the query strings containing null. You can check the value obtained from the cursor to null in a conditional expression. You can get the value in Optional. Etc. etc. - Sergey Gornostaev
  • The @WorkappWorkapp method cursor.moveToFirst returns true or false and cannot return null in any way. The result of this method also indicates whether the cursor was able to move to the first record in the result or not. By the values ​​in the columns, this method has nothing to do. - temq Nov.

1 answer 1

To check the value of a column for null use the isNull method .

 Cursor cursor = ... if(!cursor.isNull(columnIndex)){ // Получаем значение столбца по этому индексу } 
  • Thanks, this is what you need! - Howling