microflakes
microflakes

Reputation: 313

You must annotate primary keys with @NonNull?

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

Answers (4)

John Owuor
John Owuor

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

nitin gupta
nitin gupta

Reputation: 1

check the case sensitiveness it should be "@NonNull" and not "@Nonnull" import androidx.annotation.NonNull;

Upvotes: 0

otto
otto

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

Tiberiu Zulean
Tiberiu Zulean

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

Related Questions