0

On a Galaxy A6 I'm getting this stack trace:

Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.xtremecolor.mycoloring.persistance.VectorEntity.getModel()' on a null object reference
       at com.xtremecolor.mycoloring.models.VectorModelContainer.<init>(VectorModelContainer.java:29)
       at com.xtremecolor.mycoloring.mvvm.viewmodels.ColoringViewModel.<init>(ColoringViewModel.java:32)
       at java.lang.reflect.Constructor.newInstance0(Constructor.java)
       at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
       at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:267)
       at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:112)
       at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185)
       at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
       at com.xtremecolor.mycoloring.ColoringActivity.onCreate(ColoringActivity.java:105)
       at android.app.Activity.performCreate(Activity.java:7955)
       at android.app.Activity.performCreate(Activity.java:7944)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3531)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3703)
       at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
       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:2216)
       at android.os.Handler.dispatchMessage(Handler.java:107)
       at android.os.Looper.loop(Looper.java:237)
       at android.app.ActivityThread.main(ActivityThread.java:7948)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)

So I'm trying to use a singleton class "ModelsProvider" to pass an object from one activity to another as so:

@Override
public void startActivity(VectorEntity selectedVectorEntity) {

    libraryViewModel.setCurrentVectorModel(selectedVectorEntity);
    Intent coloringIntent = new Intent(getActivity(), ColoringActivity.class);
    startActivity(coloringIntent);

}

In LibraryViewModel:

public void setCurrentVectorModel(VectorEntity selectedVectorEntity){

    modelsProvider.setSelectedVectorModel(selectedVectorEntity);
}

In ModelsProvider:

public VectorEntity getSelectedVectorModel() {
    return selectedVectorEntity;
}

public void setSelectedVectorModel(VectorEntity selectedVectorModel) {
    selectedVectorEntity = selectedVectorModel;
}

In ColoringActivity's viewmodel ColoringViewModel constructor (line 32):

VectorModelContainer vectorModelContainer = new VectorModelContainer(modelsProvider.getSelectedVectorModel());

And finally in VectorModelContainter:

public VectorModelContainer(VectorEntity vectorEntity) {
    super(vectorEntity.getModel());
    this.vectorEntity = vectorEntity;
    init();
}

And right here vectorEntity.getModel() (line 29) is attempting to invoke virtual method on a null object reference. I've tried it on other devices as well, Pixel 2XL, S9, and S10, but this problem is not presenting itself on those devices.

EDIT:

Here's a stack trace from another device (Pixel 4XL) where this issues is not presenting itself:

<init>:29, VectorModelContainer (com.xtremecolor.mycoloring.models)
<init>:32, ColoringViewModel (com.xtremecolor.mycoloring.mvvm.viewmodels)
newInstance0:-1, Constructor (java.lang.reflect)
newInstance:343, Constructor (java.lang.reflect)
create:267, ViewModelProvider$AndroidViewModelFactory (androidx.lifecycle)
create:112, SavedStateViewModelFactory (androidx.lifecycle)
get:185, ViewModelProvider (androidx.lifecycle)
get:150, ViewModelProvider (androidx.lifecycle)
onCreate:115, ColoringActivity (com.xtremecolor.mycoloring)
performCreate:8000, Activity (android.app)
performCreate:7984, Activity (android.app)
callActivityOnCreate:1309, Instrumentation (android.app)
performLaunchActivity:3422, ActivityThread (android.app)
handleLaunchActivity:3601, ActivityThread (android.app)
execute:85, LaunchActivityItem (android.app.servertransaction)
executeCallbacks:135, TransactionExecutor (android.app.servertransaction)
execute:95, TransactionExecutor (android.app.servertransaction)
handleMessage:2066, ActivityThread$H (android.app)
dispatchMessage:106, Handler (android.os)
loop:223, Looper (android.os)
main:7656, ActivityThread (android.app)
invoke:-1, Method (java.lang.reflect)
run:592, RuntimeInit$MethodAndArgsCaller (com.android.internal.os)
main:947, ZygoteInit (com.android.internal.os)
AtomicallyBeyond
  • 348
  • 1
  • 15
  • What exactly do you see that makes you think the singleton is initialised? There's no apparent synchronization as well as they can be accessed from different threads. – Fred Sep 29 '21 at 06:39
  • show us the code for how models provider is saved and retrieved. And show us the code for the singleton. – Blundell Sep 29 '21 at 07:56

1 Answers1

0

I figured out why this is happening. When an Android device is low on memory the OS destroys background activities to recover memory, which makes static variables and singleton objects vulnerable to garbage collection.

Android static object lifecycle

http://www.developerphil.com/dont-store-data-in-the-application-object/

AtomicallyBeyond
  • 348
  • 1
  • 15