title: Null Pointer exception using Guice while trying to inject multiple implementations of an interface
body: <p>I'm new to Guice and dependency injection so forgive me if this is a stupid question.</p>
  <p>I'm having the following interface:</p>
  <pre><code>public interface I{
    public void handle(String message);
  }
  </code></pre>
  <p>I have the following implementations:</p>
  <pre><code>public class A implements I{
  @Override
  public void handle(String message){...}
  }
  
  public class B implements I{
  @Override
  public void handle(String message){...}
  }
  
  public class C implements I{
  @Override
  public void handle(String message){...}
  }
  </code></pre>
  <p>I have a factory method which will accept the <code>message</code>, identify it and return a concrete implementation of <code>interface I</code>. Factory method:</p>
  <pre><code>public class FactoryMethod{
  @Inject
  @Named(value=&quot;A&quot;)
  I obj_A;
  
  @Inject
  @Named(value=&quot;B&quot;)
  I obj_B;
  
  @Inject
  @Named(value=&quot;C&quot;)
  I obj_C;
  
  public I getHandler(String message){
  if (&lt;some logic-x&gt;)
    return obj_A;
  else if(&lt;some logic-y&gt;)
    return obj_B;
  return obj_C;
  }
  }
  </code></pre>
  <p>Guice module:</p>
  <pre><code>public class Module_A extends AbstractModule{
  @Override
  public void configure(){}
  
  @Provides
  @Singleton
  @Named(value=&quot;A&quot;)
  public A getA()
  {return new A();}
  </code></pre>
  <p>I have defined the same module classes for B and C as well changing the class name, Named attrib. value and object to be returned.</p>
  <p>Main module:</p>
  <pre><code>public class MainModule extends AbstractModule{
  public void configure()
  {
  install(new Module_A());
  install(new Module_B());
  install(new Module_C());
  requestInjection(this);
  }
  
  }
  </code></pre>
  <p>Injection Code:</p>
  <pre><code>public static void initialize(@NonNull RequestHandler requestHandler) {
  Injector injector = Guice.createInjector(new MainModule());
  injector.injectMembers(requestHandler);}
  </code></pre>
  <p><code>requestHandler</code> is coming from the invocation method of my project which is being invoked by an SQS event.</p>
  <p>Invocation method:</p>
  <pre><code>public class InvocationMethod implements RequestHandler&lt;SQSEvent, Void&gt; {
  public InvocationMethod(){InjectorModule.initialize(this)}
      @Override
     public Void handleRequest(SQSEvent sqsEvent, Context context){...}
  }
  </code></pre>
  <p>Problem:
  The <code>FactoryMethod.getHandler()</code> is returning null pointer which gives <code>NullPointerException</code> in the <code>InvocationMethod.handleRequest()</code> method.</p>
  <p>Any ideas how to fix this?</p>