java - @inject doesn't seem to work NPE -


i've been struggling issue while now, came here share you. first have class in wich want inject object:

public class myclass {  @javax.inject.inject  private myinterface interface        /.../ public void mymethod(){ interface.dotask(); } 

the myinterface :

public interface myinterface {      public abstract void dotask() throws exception; } 

is interface wich bind implementation :

public class myinterfaceimpl implements myinterface{   @inject    public myinterfaceimpl(...) {        /.../     }   @override    public void dotask() throws exception{       /.../ } 

in config:

public class applicationconfig extends resourceconfig {  private config config = new config();      public applicationconfig() {         super();         register(new mainbinder()); }     private class mainbinder extends abstractbinder {          @override          protected void configure() {       bind(myinterfaceimpl.class).to(myinterface.class).in(singleton.class);         }     } } 

so, when run app , try execute method have npe on :

interface.dotask(); 

voilĂ , sorry ask need on that, plus i've tried generic possible hoping didn't impact comprehension. in advance!

edit: forgot mention class myclass() called in class : new myclass()

so think problem might there.

so figure out! creating new instance of myclass => new myclass() injection couldn't work! injected myclass() instead of creating new instance , bound in applicationconfig. worked fine.


Comments