Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot auto upgrade a database with a non null value #824

Closed
ksdc-anandhid opened this issue Feb 3, 2020 · 4 comments
Closed

Cannot auto upgrade a database with a non null value #824

ksdc-anandhid opened this issue Feb 3, 2020 · 4 comments
Labels
more info required Further information is requested

Comments

@ksdc-anandhid
Copy link

ksdc-anandhid commented Feb 3, 2020

I have an Android class with the following fields:

public class Player {
    @Id
    private long id;
    private String name;
    private int  jerseyNumber;
}

The above code works fine.
For the auto-upgrade, I add a new non-null property named code like the following:

public class Player {
    @Id
    private long id;
    private String name;
    private int jerseyNumber;
    @Nonnull
    private String code;
}

There is a null pointer exception as the database is trying to be upgraded as Objectbox was not able to insert a non-null code for the existing rows.

Please advise on a solution.

@greenrobot-team
Copy link
Member

ObjectBox ignores @Nonnull annotations. How does the constructor or the setter look like? Does it do a null check?

Could you provide the stack trace of the crash?

@greenrobot-team greenrobot-team added the more info required Further information is requested label Feb 3, 2020
@ksdc-anandhid
Copy link
Author

I use Lombok and so my code looks like the below

@DaTa
@NoArgsConstructor
@AllArgsConstructor

@entity
public class Player {
@id
private long id;
private String name;
private int jerseyNumber;
@nonnull
private String code;
}

2020-02-03 15:01:25.593 30716-30716/com.example.objectboxdemo E/Box: Entity creation failed
2020-02-03 15:01:25.672 30716-30716/com.example.objectboxdemo A/zygote64: java_vm_ext.cc:534] JNI DETECTED ERROR IN APPLICATION: JNI CallBooleanMethodV called with pending exception java.lang.NullPointerException: code is marked non-null but is null
2020-02-03 15:01:25.673 30716-30716/com.example.objectboxdemo A/zygote64: java_vm_ext.cc:534] at void com.example.objectboxdemo.model.Player.(long, java.lang.String, int, java.lang.String) (Player.java:19)
2020-02-03 15:01:25.673 30716-30716/com.example.objectboxdemo A/zygote64: java_vm_ext.cc:534] at java.util.List io.objectbox.query.Query.nativeFind(long, long, long, long) (Query.java:-2)
2020-02-03 15:01:25.673 30716-30716/com.example.objectboxdemo A/zygote64: java_vm_ext.cc:534] at java.util.List io.objectbox.query.Query$3.call() (Query.java:209)
2020-02-03 15:01:25.673 30716-30716/com.example.objectboxdemo A/zygote64: java_vm_ext.cc:534] at java.lang.Object io.objectbox.query.Query$3.call() (Query.java:206)
2020-02-03 15:01:25.673 30716-30716/com.example.objectboxdemo A/zygote64: java_vm_ext.cc:534] at java.lang.Object io.objectbox.BoxStore.callInReadTx(java.util.concurrent.Callable) (BoxStore.java:759)
2020-02-03 15:01:25.673 30716-30716/com.example.objectboxdemo A/zygote64: java_vm_ext.cc:534] at java.lang.Object io.objectbox.BoxStore.callInReadTxWithRetry(java.util.concurrent.Callable, int, int, boolean) (BoxStore.java:704)
2020-02-03 15:01:25.673 30716-30716/com.example.objectboxdemo A/zygote64: java_vm_ext.cc:534] at java.lang.Object io.objectbox.query.Query.callInReadTx(java.util.concurrent.Callable) (Query.java:293)
2020-02-03 15:01:25.673 30716-30716/com.example.objectboxdemo A/zygote64: java_vm_ext.cc:534] at java.util.List io.objectbox.query.Query.find() (Query.java:206)

@greenrobot-team
Copy link
Member

greenrobot-team commented Feb 3, 2020

Looks like the constructor generated by your tooling does do a null check. However, ObjectBox will pass null as that's what is in the database for code of existing entities: nothing.

Two options:

  • run some upgrade code which explicitly stores a default value, or
  • ensure the constructor can be called with a null value for code.

As for ObjectBox supporting default values look at #157.

@ksdc-anandhid
Copy link
Author

Hi,
I used the option 2 and added a not null constructor. It works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
more info required Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants