question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dagger Application injection NPE

See original GitHub issue

I’m trying to inject some objects on application, in order to access them whenever I want. So, on my application I’m doing something like this:

private static AppComponent mApplicationComponent;

@Override
    public void onCreate() {
        …
       mApplicationComponent = DaggerAppComponent.builder()
                .appModule(new AppModule(this, new ModuleSpref(this))
                .build();
   }

 public static AppComponent getInstance() {
        return mApplicationComponent;
    }

However, according to google crash log of my production app, the application has a NullPointException when trying to access anything from getInstance(). On my application this happens on a service which is only called via a receiver. Additionally, this also happens on another receiver.

 MyApplication.getInstance().context(); //Null Point Exception :(

Here is my ApplicationComponent:

@Component(modules = AppModule.class)
@Singleton
public interface AppComponent {
    void inject(Context context);

    //Exposed to sub-graphs.
    Context context();
    ModuleSpref getSpref();
}

I tried many times to replicate, but so far without success, despite the google crash saying otherwise. Am I doing something wrong here? As far as I know, for services/receivers to be initialised in android, the application must first be initialised, so getInstance() should not return null right?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:31 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
JakeWhartoncommented, Oct 17, 2017

No

On Tue, Oct 17, 2017, 12:12 PM Ron Shapiro notifications@github.com wrote:

@Zhuinden https://github.com/zhuinden is this what you’re referring too: square/mortar@bf74e3e https://github.com/square/mortar/commit/bf74e3e0ad00713c3123b957a61f82bd73b578b5 ?

Is there any reason to not just call (Application) activity.getApplicationContext() here then and not worry about the null-check?

/cc @rjrjr https://github.com/rjrjr @loganj https://github.com/loganj

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/google/dagger/issues/748#issuecomment-337272449, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEWPEtAeiU80PqYMRPwQN_rnusoFcks5stNHzgaJpZM4NjrJU .

5reactions
Zhuindencommented, Jul 15, 2020

@camsteffen if I understand the SO solution, it is just moving the component to a static variable? This would work for the production app, but keeping your DI graph in static state tends to cause problems for things like tests.

That’s never bothered anyone using Koin, which works exactly the same way.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Dagger 2 - Null Pointer in Field Injection - Stack Overflow
I am using dagger 2 for dependency injection and got stuck with field injection. Below is the complete scenario with code sample:.
Read more >
Dagger & Android
Dagger is a fully static, compile-time dependency injection framework for both Java and Android. It is developed by the Java Core Libraries Team...
Read more >
Using Dagger in your Android app - Kotlin
We'll use Dagger as the DI tool to manage dependencies. Dependency injection (DI) is a technique widely used in programming and well suited...
Read more >
That Missing Guide: How to use Dagger2 | by Gabor Varadi
Dagger is a dependency injection framework, which makes it easier to manage the dependencies between the classes in our app.
Read more >
Dependency injection with Dagger 2 - Custom scopes
Scopes - practical example · @Singleton - application scope · @UserScope - scope for classes instances associated with picked user (in real app...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found