Dagger 2 - Null Pointer in Field Injection










0















I am using dagger 2 for dependency injection and got stuck with field injection. Below is the complete scenario with code sample:



Let's say we have a class A which depends on a library B



class A 
@Inject
B b;



Module for B:



@Module
public class BModule

@Provides
@Singleton
public B provideB()
return new C.methodA();
// C - static class; C.methodA returns B





But when I try to use b in class A then I get null pointer exception but if I do the same using constructor injection then it works perfectly.
I can assure that component and other dependencies are fine as the constructor part works correctly.



A is a dependency of some other class (let's call X) and A is being initialized using constructor injection (tested). Also, X is being injected as void inject(X x);



I have 2 questions:



  1. Is there anything I am missing out for field injection due to which it is not being injected?

  2. I am able to successfully compile the code and get runtime exception, but dagger2 is compile-time DI then why is it unable to catch this while compiling?

P.S.: I have just shared a part of the code as there are multiple dependencies, so just trying to explain the scenario. Let me know if the question/scenario is still unclear or needs more info.



Thanks.










share|improve this question




























    0















    I am using dagger 2 for dependency injection and got stuck with field injection. Below is the complete scenario with code sample:



    Let's say we have a class A which depends on a library B



    class A 
    @Inject
    B b;



    Module for B:



    @Module
    public class BModule

    @Provides
    @Singleton
    public B provideB()
    return new C.methodA();
    // C - static class; C.methodA returns B





    But when I try to use b in class A then I get null pointer exception but if I do the same using constructor injection then it works perfectly.
    I can assure that component and other dependencies are fine as the constructor part works correctly.



    A is a dependency of some other class (let's call X) and A is being initialized using constructor injection (tested). Also, X is being injected as void inject(X x);



    I have 2 questions:



    1. Is there anything I am missing out for field injection due to which it is not being injected?

    2. I am able to successfully compile the code and get runtime exception, but dagger2 is compile-time DI then why is it unable to catch this while compiling?

    P.S.: I have just shared a part of the code as there are multiple dependencies, so just trying to explain the scenario. Let me know if the question/scenario is still unclear or needs more info.



    Thanks.










    share|improve this question


























      0












      0








      0








      I am using dagger 2 for dependency injection and got stuck with field injection. Below is the complete scenario with code sample:



      Let's say we have a class A which depends on a library B



      class A 
      @Inject
      B b;



      Module for B:



      @Module
      public class BModule

      @Provides
      @Singleton
      public B provideB()
      return new C.methodA();
      // C - static class; C.methodA returns B





      But when I try to use b in class A then I get null pointer exception but if I do the same using constructor injection then it works perfectly.
      I can assure that component and other dependencies are fine as the constructor part works correctly.



      A is a dependency of some other class (let's call X) and A is being initialized using constructor injection (tested). Also, X is being injected as void inject(X x);



      I have 2 questions:



      1. Is there anything I am missing out for field injection due to which it is not being injected?

      2. I am able to successfully compile the code and get runtime exception, but dagger2 is compile-time DI then why is it unable to catch this while compiling?

      P.S.: I have just shared a part of the code as there are multiple dependencies, so just trying to explain the scenario. Let me know if the question/scenario is still unclear or needs more info.



      Thanks.










      share|improve this question
















      I am using dagger 2 for dependency injection and got stuck with field injection. Below is the complete scenario with code sample:



      Let's say we have a class A which depends on a library B



      class A 
      @Inject
      B b;



      Module for B:



      @Module
      public class BModule

      @Provides
      @Singleton
      public B provideB()
      return new C.methodA();
      // C - static class; C.methodA returns B





      But when I try to use b in class A then I get null pointer exception but if I do the same using constructor injection then it works perfectly.
      I can assure that component and other dependencies are fine as the constructor part works correctly.



      A is a dependency of some other class (let's call X) and A is being initialized using constructor injection (tested). Also, X is being injected as void inject(X x);



      I have 2 questions:



      1. Is there anything I am missing out for field injection due to which it is not being injected?

      2. I am able to successfully compile the code and get runtime exception, but dagger2 is compile-time DI then why is it unable to catch this while compiling?

      P.S.: I have just shared a part of the code as there are multiple dependencies, so just trying to explain the scenario. Let me know if the question/scenario is still unclear or needs more info.



      Thanks.







      java dependency-injection dagger-2 dagger dagger-android






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 19:47







      divyum

















      asked Nov 14 '18 at 18:11









      divyumdivyum

      780818




      780818






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Field injection in dagger is a bit more complicated than constructor injection. When you use constructor injection like this



          class A 
          @Inject
          public A(B b)



          and you have provider for class B



          @Module
          class DaggerModule
          @Provides
          B provideB()



          now dagger will know how to create instance of A and pass it required constructor parameter. So everything is fine, compile successfully and works perfect.



          But if we speak about field injection



          class A 
          @Inject
          B b;



          and have somewhere provider for B, dagger can't know how to create A instance and when inject b property (in case of manually creating instance of A by hand). To make it work you need write special method in component



          @Component(DaggetModule.class)
          interface DaggerComponent
          void inject(A a);



          and somewhere in code



          A a = new A();
          DaggerComponent component = //TODO getDaggerComponent()
          component.inject(a);


          After that b property will be initialized and available for later usage. Hope, it's clear now how to make field injection work.






          share|improve this answer























          • Thanks @ConstOrVar, I forgot to mention here that A is a dependency of some other class (let's call X) and A is being initialized correctly. Also, X is being initialized as you have suggested i.e. void inject(X x); I will update in the question as well.

            – divyum
            Nov 14 '18 at 19:44










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306403%2fdagger-2-null-pointer-in-field-injection%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Field injection in dagger is a bit more complicated than constructor injection. When you use constructor injection like this



          class A 
          @Inject
          public A(B b)



          and you have provider for class B



          @Module
          class DaggerModule
          @Provides
          B provideB()



          now dagger will know how to create instance of A and pass it required constructor parameter. So everything is fine, compile successfully and works perfect.



          But if we speak about field injection



          class A 
          @Inject
          B b;



          and have somewhere provider for B, dagger can't know how to create A instance and when inject b property (in case of manually creating instance of A by hand). To make it work you need write special method in component



          @Component(DaggetModule.class)
          interface DaggerComponent
          void inject(A a);



          and somewhere in code



          A a = new A();
          DaggerComponent component = //TODO getDaggerComponent()
          component.inject(a);


          After that b property will be initialized and available for later usage. Hope, it's clear now how to make field injection work.






          share|improve this answer























          • Thanks @ConstOrVar, I forgot to mention here that A is a dependency of some other class (let's call X) and A is being initialized correctly. Also, X is being initialized as you have suggested i.e. void inject(X x); I will update in the question as well.

            – divyum
            Nov 14 '18 at 19:44















          1














          Field injection in dagger is a bit more complicated than constructor injection. When you use constructor injection like this



          class A 
          @Inject
          public A(B b)



          and you have provider for class B



          @Module
          class DaggerModule
          @Provides
          B provideB()



          now dagger will know how to create instance of A and pass it required constructor parameter. So everything is fine, compile successfully and works perfect.



          But if we speak about field injection



          class A 
          @Inject
          B b;



          and have somewhere provider for B, dagger can't know how to create A instance and when inject b property (in case of manually creating instance of A by hand). To make it work you need write special method in component



          @Component(DaggetModule.class)
          interface DaggerComponent
          void inject(A a);



          and somewhere in code



          A a = new A();
          DaggerComponent component = //TODO getDaggerComponent()
          component.inject(a);


          After that b property will be initialized and available for later usage. Hope, it's clear now how to make field injection work.






          share|improve this answer























          • Thanks @ConstOrVar, I forgot to mention here that A is a dependency of some other class (let's call X) and A is being initialized correctly. Also, X is being initialized as you have suggested i.e. void inject(X x); I will update in the question as well.

            – divyum
            Nov 14 '18 at 19:44













          1












          1








          1







          Field injection in dagger is a bit more complicated than constructor injection. When you use constructor injection like this



          class A 
          @Inject
          public A(B b)



          and you have provider for class B



          @Module
          class DaggerModule
          @Provides
          B provideB()



          now dagger will know how to create instance of A and pass it required constructor parameter. So everything is fine, compile successfully and works perfect.



          But if we speak about field injection



          class A 
          @Inject
          B b;



          and have somewhere provider for B, dagger can't know how to create A instance and when inject b property (in case of manually creating instance of A by hand). To make it work you need write special method in component



          @Component(DaggetModule.class)
          interface DaggerComponent
          void inject(A a);



          and somewhere in code



          A a = new A();
          DaggerComponent component = //TODO getDaggerComponent()
          component.inject(a);


          After that b property will be initialized and available for later usage. Hope, it's clear now how to make field injection work.






          share|improve this answer













          Field injection in dagger is a bit more complicated than constructor injection. When you use constructor injection like this



          class A 
          @Inject
          public A(B b)



          and you have provider for class B



          @Module
          class DaggerModule
          @Provides
          B provideB()



          now dagger will know how to create instance of A and pass it required constructor parameter. So everything is fine, compile successfully and works perfect.



          But if we speak about field injection



          class A 
          @Inject
          B b;



          and have somewhere provider for B, dagger can't know how to create A instance and when inject b property (in case of manually creating instance of A by hand). To make it work you need write special method in component



          @Component(DaggetModule.class)
          interface DaggerComponent
          void inject(A a);



          and somewhere in code



          A a = new A();
          DaggerComponent component = //TODO getDaggerComponent()
          component.inject(a);


          After that b property will be initialized and available for later usage. Hope, it's clear now how to make field injection work.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 19:18









          ConstOrVarConstOrVar

          1,094149




          1,094149












          • Thanks @ConstOrVar, I forgot to mention here that A is a dependency of some other class (let's call X) and A is being initialized correctly. Also, X is being initialized as you have suggested i.e. void inject(X x); I will update in the question as well.

            – divyum
            Nov 14 '18 at 19:44

















          • Thanks @ConstOrVar, I forgot to mention here that A is a dependency of some other class (let's call X) and A is being initialized correctly. Also, X is being initialized as you have suggested i.e. void inject(X x); I will update in the question as well.

            – divyum
            Nov 14 '18 at 19:44
















          Thanks @ConstOrVar, I forgot to mention here that A is a dependency of some other class (let's call X) and A is being initialized correctly. Also, X is being initialized as you have suggested i.e. void inject(X x); I will update in the question as well.

          – divyum
          Nov 14 '18 at 19:44





          Thanks @ConstOrVar, I forgot to mention here that A is a dependency of some other class (let's call X) and A is being initialized correctly. Also, X is being initialized as you have suggested i.e. void inject(X x); I will update in the question as well.

          – divyum
          Nov 14 '18 at 19:44



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306403%2fdagger-2-null-pointer-in-field-injection%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3

          Museum of Modern and Contemporary Art of Trento and Rovereto