WebApplicationInitializer container.addServlet() returns null
eclipse, spring-data, web-applications
Solution
According to ServletContext JavaDoc the method addServlet() will return null if a servlet with the specified name is already registered.
Problem
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. I am following the code-based configuration approach found here: WebApplicationInitializer 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. What the heck am I missing? I am a bit new to creating web-apps from scratch using annotations. Here is the class in question: ``` 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("/*"); } } ``` Tried adding this to the POM: ``` <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> ``` Didn't change anything, still getting the NPE. I read here (http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html) that container.addServlet returns null if a servlet is already registered? Is Tomcat registering a servlet already? Apologies for wasting everyone's time, I had a web.xml file also registering the same servlet. So this one was only returning null. Now on to fixing the 404, probably screwed up the controller somehow.