How to annotate Column as NOT NULL using Android Room Persistence Library

For Java you should annotate with @android.support.annotation.NonNull

DO NOT CONFUSE WITH @io.reactivex.annotations.NonNull


For Java I used @NonNull after the @ColumnInfo:

@ColumnInfo(name = "column_name")
@NonNull private String str;

Ensure that you have imported the correct library in the class:

import android.support.annotation.NonNull;

In order to identify exact annotation for NOT NULL I went through the list of all annotations provided at this link: android.arch.persistence.room but could not find any relevant annotation:

enter image description here

My assumption is that since you are using Kotlin, which by default treats a var as non-optional, all the attributes declared by you are NOT NULL. In order to declare a var as optional probably you have to use a ? operator, an example below:

var name: String // NOT NULL 
var name: String? // OPTIONAL

Updated based on comment:

I guess you are confused with NULL and empty string. NULL means no value, in the shared code you are providing a default value to each column which is an empty string and that is not equal to NULL, so even though you might not have provided a value for that attribute it is by default assigning an empty string to it.

Try removing value assignment operator and RHS value for an attribute and then insert the record without assigning value to that attribute, you should get appropriate error.

Basically, convert below statement:

@ColumnInfo(name = "name") var name: String = ""

To

@ColumnInfo(name = "name") var name: String