Reputation: 313
I'm getting this error on this piece of code. You must annotate primary keys with @NonNull. "uidString" is nullable.
When I annotate it with @NonNull and @PrimaryKey, An entity must have at least 1 field annotated with @PrimaryKey
What's up?
import androidx.annotation.NonNull;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
import java.util.UUID;
@Entity(tableName = "player_profiles")
public class PlayerGameProfile implements Parcelable {
@PrimaryKey
public String uidString = UUID.randomUUID().toString();
@ColumnInfo(name = "name")
public String name;
Upvotes: 10
Views: 7114
Reputation: 113
In my case I had:
@PrimaryKey @NonNull var reasonId: String? = Constants.EMPTY_STRING,
instead of:
@PrimaryKey @NonNull var reasonId: String = Constants.EMPTY_STRING,
Upvotes: 2
Reputation: 1
check the case sensitiveness it should be "@NonNull" and not "@Nonnull" import androidx.annotation.NonNull;
Upvotes: 0
Reputation: 190
For Java you should annotate with @android.support.annotation.NonNull
Delete the other NonNull import you are using yet and change it to
import android.support.annotation.NonNull;
Then use both annotations
@PrimaryKey
@NonNull
Upvotes: 6
Reputation: 172
You can try binding the primary key with the Entity like this:
@Entity(tableName = "player_profiles",
primaryKeys = { "uidString" })
After that annotate your primary key with @NonNull
in the class below.
@NonNull
public String uidString = UUID.randomUUID().toString();
Upvotes: 0