1

I have a class that should process all notifications and therefore extends FirebaseMessagingService:

class DefaultFCMHandler(): FirebaseMessagingService(), FCMHandler, KodeinAware {

    private val _kodein: Kodein by closestKodein()

    override val kodein = Kodein.lazy {
        extend(_kodein)
    }

    private val authenticator: Authenticator by instance()

    override fun onNewToken(token: String) {
        // ...
    }

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        // ...
    }
}

In the application class I bind everything as follows:

class MyApplication : Application(), KodeinAware {

    override val kodein = Kodein.lazy {
        // ...
        bind<Authenticator>() with singleton {
            DefaultAuthenticator(
                instance(),
                instance(),
                instance()
            )
        }
        // ...
        bind<FCMHandler>() with singleton {
            DefaultFCMHandler()
        }
    }
}

The problem is that whatever I try I always get the following exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference

I don't understand that because based on this it should have access to a context (I put the application file into the manifest).

I found this on github but the promoted solution doesn't fix the problem.

I probably have a general misconception of how the DI works.

The full stacktrace is as follows:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: de.gitlab.user, PID: 14300
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
        at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:118)
        at org.kodein.di.android.ClosestKt.kodein(closest.kt:25)
        at org.kodein.di.android.ClosestKt.access$kodein(closest.kt:1)
        at org.kodein.di.android.ContextKodeinPropertyDelegateProvider$provideDelegate$1.invoke(closest.kt:39)
        at org.kodein.di.android.ContextKodeinPropertyDelegateProvider$provideDelegate$1.invoke(closest.kt:38)
        at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
        at de.gitlab.user.data.fcm.DefaultFCMHandler.get_kodein(Unknown Source:7)
        at de.gitlab.user.data.fcm.DefaultFCMHandler.access$get_kodein$p(DefaultFCMHandler.kt:16)
        at de.gitlab.user.data.fcm.DefaultFCMHandler$kodein$1.invoke(DefaultFCMHandler.kt:21)
        at de.gitlab.user.data.fcm.DefaultFCMHandler$kodein$1.invoke(DefaultFCMHandler.kt:16)
        at org.kodein.di.internal.KodeinImpl$Companion.newBuilder(KodeinImpl.kt:22)
        at org.kodein.di.internal.KodeinImpl$Companion.access$newBuilder(KodeinImpl.kt:21)
        at org.kodein.di.internal.KodeinImpl.<init>(KodeinImpl.kt:19)
        at org.kodein.di.Kodein$Companion$lazy$1.invoke(Kodein.kt:447)
        at org.kodein.di.Kodein$Companion$lazy$1.invoke(Kodein.kt:429)
        at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
        at org.kodein.di.LazyKodein.getBaseKodein(Unknown Source:7)
        at org.kodein.di.LazyKodein.getContainer(lateinit.kt:31)
        at org.kodein.di.KodeinAwareKt$Instance$1.invoke(KodeinAware.kt:176)
        at org.kodein.di.KodeinAwareKt$Instance$1.invoke(Unknown Source:4)
        at org.kodein.di.KodeinProperty$provideDelegate$1.invoke(properties.kt:42)
        at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
        at de.gitlab.user.data.fcm.DefaultFCMHandler.getAuthenticator(Unknown Source:7)
        at de.gitlab.user.data.fcm.DefaultFCMHandler.sendRegistrationToServer(DefaultFCMHandler.kt:74)
        at de.gitlab.user.data.fcm.DefaultFCMHandler.access$sendRegistrationToServer(DefaultFCMHandler.kt:16)
        at de.gitlab.user.data.fcm.DefaultFCMHandler$submitToken$1.onComplete(DefaultFCMHandler.kt:50)
        at com.google.android.gms.tasks.zzj.run(Unknown Source:4)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
F.M.F.
  • 1,929
  • 3
  • 23
  • 42
  • 1
    Where does the crash happen ? Do you have a stack trace ? or even better a reproducible project to checkout ? – romainbsl Oct 25 '19 at 07:28
  • @romainbsl I added the stacktrace – F.M.F. Oct 25 '19 at 09:41
  • 1
    Is `ApplicationContext` one of the parameters of `DefaultAuthenticator` defined as `instance()` ? if this is the case you should have a binding with `ApplicationContext` so that kodein would be able to use it – romainbsl Nov 04 '19 at 07:47

0 Answers0