title: WebApplicationInitializer container.addServlet() returns null
body: <p>I am creating a basic web app with maven, then importing to Eclipse 4.2.  I have Tomcat 7 setup as a server.  I am trying to configure spring data with mongodb for a web app.  </p>
  
  <p>I am following the code-based configuration approach found here: <a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/WebApplicationInitializer.html">WebApplicationInitializer</a></p>
  
  <p>When I run the project on the server, I get a null pointer exception in the WebApplicationInitializer class I have created.  The line: 
  container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); is returning null.</p>
  
  <p>What the heck am I missing?  I am a bit new to creating web-apps from scratch using annotations. </p>
  
  <p>Here is the class in question:</p>
  
  <pre><code>public class ATWWebAppInitializer implements WebApplicationInitializer
  {
      @Override
      public void onStartup(ServletContext container) throws ServletException  
      {
        // Create the 'root' Spring application context
        AnnotationConfigWebApplicationContext rootContext = new  AnnotationConfigWebApplicationContext();
        rootContext.register(SpringMongoConfig.class);
  
        // Manage the lifecycle of the root application context
        container.addListener(new ContextLoaderListener(rootContext));
  
        // Create the dispatcher servlet's Spring application context
        AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
        dispatcherContext.register(ATWDispatcherConfig.class);
  
        // Register and map the dispatcher servlet
        ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/*");
      }
  } 
  </code></pre>
  
  <p>Tried adding this to the POM:</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
      &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
      &lt;version&gt;3.0.1&lt;/version&gt;
      &lt;scope&gt;provided&lt;/scope&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>Didn't change anything, still getting the NPE.  I read here (<a href="http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html">http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html</a>) that container.addServlet returns null if a servlet is already registered?  Is Tomcat registering a servlet already?</p>
  
  <p>Apologies for wasting everyone's time, <strong>I had a web.xml file also registering the same servlet.</strong>  So this one was only returning null.  Now on to fixing the 404, probably screwed up the controller somehow.</p>