-
Notifications
You must be signed in to change notification settings - Fork 45
Can't read data of LocalDateTime object from MicroStream database on Android #245
Description
Environment Details
- MicroStream Version: 05.00.02-MS-GA
- JDK version: JDK 11
- OS: Android API Level 30 (Minimum: 26 - Target: 31)
Describe the bug
I've been attempting to implement a MicroStream database in an Android app in order to implement a offline storage. I have a object that for example contains various strings, other objects and a LocalDateTime object. While I'm able to save all data just fine, read attempts end up returning the Date and Time inside a LocalDateTime objects as null.
I've attempted to reproduce it in a pretty much identical Java project, however it appears to be working just fine. It's just Android that seems to be affected by this.
To Reproduce
- Save an object that contains a LocalDateTime object
- Restart the app (not sure if this is required, but i've always included this step)
- Attempt to read the object
- Detect that the Time and Date of the LocalDateTime object are null
Expected behavior
Data is readable on both Java and Android
Additional context
Example code for a android project (located inside an activity's OnCreate method):
DataRoot dataRoot = new DataRoot();
EmbeddedStorageManager storageManager = EmbeddedStorage.start(dataRoot, getFilesDir().toPath());
storageManager.setRoot(dataRoot);
//Step 1: Save Data - Comment this after first launch
dataRoot.setLdt(LocalDateTime.now());
dataRoot.setTxt("Example text");
storageManager.storeRoot();
//Step 2: Read Data - Uncomment this after first launch
/*System.out.println("Saved text: " + dataRoot.getTxt());
System.out.println("Saved LocalDateTime: " + dataRoot.getLdt());*/
I've used the identical code to test this in a Java project, however I replaced getFilesDir().toPath() with a differnet path as that's a Android exclusive method. The DataRoot class is just a simple class that contains a empty constructor, two variables (one LocalDateTime and one String) and automatically generated default Getters and Setters for these variables.
Java console output during the second step:
Saved text: Example text
Saved LocalDateTime: 2021-09-16T15:49:47.017022600
Android console output during the second step:
I/System.out: Saved text: Example text
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.xdevsoftware.mstestapp, PID: 8487
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.xdevsoftware.mstestapp/de.xdevsoftware.mstestapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.time.LocalDate.toString()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.time.LocalDate.toString()' on a null object reference
at java.time.LocalDateTime.toString(LocalDateTime.java:1966)
at java.lang.String.valueOf(String.java:2924)
at java.lang.StringBuilder.append(StringBuilder.java:132)
at de.xdevsoftware.mstestapp.MainActivity.onCreate(MainActivity.java:32)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
MainActivity.java:32 refers to the last line in the code example.