projectlombok / lombok

Very spicy additions to the Java programming language.
https://projectlombok.org/
Other
12.82k stars 2.38k forks source link

[BUG] Break Room @PrimaryKey(autoGenerate = true) #2802

Open firdausmaulan opened 3 years ago

firdausmaulan commented 3 years ago

Describe the bug I'm using Lombok & Room in my android project Everything going well, until I update Lombok to version 1.18.16 It's look like new version not compatible with Room annotation @PrimaryKey(autoGenerate = true) And it's throw error : java.lang.NullPointerException: id is marked non-null but is null

To Reproduce

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity(indices = {
        @Index(value = "id", unique = true)
}, tableName = "t_sync")
public class Sync extends BaseModel {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    @NonNull
    private Long id;

    ... other properites

}

Just insert the data and crash

Expected behavior

Version info (please complete the following information):

Additional context https://developer.android.com/jetpack/androidx/releases/room

Rawi01 commented 3 years ago

@NonNull generates a null-check and if you set one value to null that check throws an exception. Is it possible that you accidentally imported the lombok annotation instead of a specific Room/android annotation?

firdausmaulan commented 3 years ago

I've check my code, and I imported from androidx room.

import androidx.room.PrimaryKey;

I just downgrade lombok version to 1.18.10 and everything going well.

Rawi01 commented 3 years ago

The problem is that you added a @NonNull annotation to a field and try to assign null to that field. Lombok detects the annotation and generates code that prevents null values. This is intended. Simply remove the annotation and it should work.

Edit: I think I should explain a little bit why you should remove the @NonNull annotation even if you would not use lombok at all. @PrimaryKey(autoGenerate = true) generates a primary key if the Long field is null. At the same time you added a @NonNull annotation (I guess you used androidx.annotation.NonNull) which indicates that the field can never be null. You cannot get both things at the same time because you need null for autoGenerate = true.