Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 421
MattG Avatar asked Aug 28 '13 22:08

MattG


2 Answers

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

like image 156
Ravi Rao Avatar answered Nov 20 '22 21:11

Ravi Rao


@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

like image 1
Amol Dixit Avatar answered Nov 20 '22 19:11

Amol Dixit