1

I am writing a program in which i have two LiveData objects as shown:


    public LiveData<placeImages> chosenImages;
    public LiveData<List<Bitmap>> chosenBitmaps= Transformations.map(chosenImages, (placeimags) ->
    {
        ArrayList<Bitmap> bitmaps=new ArrayList<Bitmap>();
        for(PhotoI photoI:placeimags.images)
        {
            try {
                Bitmap b=getThumbnail(app.getContentResolver(),photoI.imageURI);
                if(b!=null) {
                    bitmaps.add(b);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return bitmaps;
    });

The problem is whenever I observe the chosenImages LiveData it works fine, but when I observe chosenBitmaps LiveData it returns the following error:

2020-07-18 17:40:39.790 32164-32164/com.nisarg.locsav E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.nisarg.locsav, PID: 32164
    java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.lifecycle.LiveData.observeForever(androidx.lifecycle.Observer)' on a null object reference
        at androidx.lifecycle.MediatorLiveData$Source.plug(MediatorLiveData.java:141)
        at androidx.lifecycle.MediatorLiveData.onActive(MediatorLiveData.java:118)
        at androidx.lifecycle.LiveData$ObserverWrapper.activeStateChanged(LiveData.java:437)
        at androidx.lifecycle.LiveData$LifecycleBoundObserver.onStateChanged(LiveData.java:395)
        at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:361)
        at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:300)
        at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:339)
        at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:145)
        at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:131)
        at androidx.fragment.app.FragmentViewLifecycleOwner.handleLifecycleEvent(FragmentViewLifecycleOwner.java:51)
        at androidx.fragment.app.Fragment.performStart(Fragment.java:2737)
        at androidx.fragment.app.FragmentStateManager.start(FragmentStateManager.java:365)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1194)
        at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
        at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
        at android.os.Handler.handleCallback(Handler.java:794)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:6635)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

I think Transformations.map() is returning null value. I worked on a similar thing in a project before, same thing happened when i used Transformations.map(). Does anyone know why this is happening?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Your `chosenImages` is not initialized when you call `Transformations.map`, so it improperly tries to attach to `null` when you observe it. – Pawel Jul 18 '20 at 12:59
  • @Pawel What do i do to solve this problem? – neonpokharkar Jul 18 '20 at 15:29
  • @Pawel I have the same problem, but in my case I do initialize the referred `LiveData` (Kotlin code: `private val userDocument = MutableLiveData() val userData = Transformations.map(userDocument) { it?.toObject(UserModel::class.java) }`). Any ideas? – Omer Levy Jul 25 '21 at 15:14

2 Answers2

1

Presumably there is a MutableLiveData somewhere behind chosenImages?

What you describe happens when that MutableLiveData is not initialized by using the noargs constructor new MutableLiveData<placeImages>(), leaving the Transformations.map() derived LiveData also not initialized, hence the null.

The solution is to initialize the MutableLiveData on creation, even if it is initialized to null: new MutableLiveData<placeImages>(null) so that the derived also gets initialized.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
kewCogs
  • 21
  • 3
  • 1
    Also worth adding, you need to 'observe' your Transformations.map()'d LiveData for it to register a value, something I discovered in unit testing where I had been just checking the LiveData's .getValue() – kewCogs Dec 24 '21 at 19:25
0

change public LiveData<placeImages> chosenImages; to

public LiveData<placeImages> chosenImages = MutableLiveData()

Jeff Padgett
  • 2,380
  • 22
  • 34
  • I have the same problem, but in my case I do initialize the referred `LiveData` (Kotlin code: `private val userDocument = MutableLiveData() val userData = Transformations.map(userDocument) { it?.toObject(UserModel::class.java) }`). Any ideas? – Omer Levy Jul 25 '21 at 15:15