MattG
MattG

Reputation: 113

WebApplicationInitializer container.addServlet() returns null

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.

Upvotes: 9

Views: 3563

Answers (2)

Amol Dixit
Amol Dixit

Reputation: 669

@Ravi Rao - You are precisely correct. I was struggling hard to run my app on the tomcat and was getting the NPE exactly at dispatcher.setLoadOnStartup().

But when I read the answer you provided, I realized that there was another application running on the same tomcat with the same name of that dispatcher servlet.

I just renamed my dispatcher servlet and it worked amazingly. Just mentioned the exact steps to resolve this issue. In case someone find it useful

Upvotes: 1

Ravi Rao
Ravi Rao

Reputation: 401

According to ServletContext JavaDoc the method addServlet() will return null if a servlet with the specified name is already registered.

Upvotes: 13

Related Questions