You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Nick Williams <ni...@nicholaswilliams.net> on 2013/03/14 20:56:30 UTC

NullPointerException in MapperListener; Tomcat#start() does not create a Container?

Using a variety of tutorials I found online and the documentation for o.a.c.startup.Tomcat, I created the following main method to start up an embedded Tomcat. I'm using 7.0.37 Tomcat JARs.

    public static void main(String... arguments) throws Exception
    {
        Tomcat tomcat = new Tomcat();
        tomcat.setBaseDir(".basedir");
        tomcat.setPort(8973);
        tomcat.enableNaming();
        tomcat.init();
        tomcat.start();

        System.out.println("X: " + tomcat.getConnector().getService().getContainer());

        tomcat.getServer().await();
    }

The System.out.println is for debugging purposes, because I'm getting a NullPointerException. Obviously I'm doing something wrong, because about an hour of Googling turned up precisely zero results of anyone who's getting a NullPointerException in MapperListener#findDefaultHost. For some reason, it looks like a Container is never created. What gives? Here's the full output of running the JAR file:

Mar 14, 2013 2:39:04 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8973"]
Mar 14, 2013 2:39:04 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Tomcat
Mar 14, 2013 2:39:04 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8973"]
Mar 14, 2013 2:39:04 PM org.apache.catalina.core.StandardService startInternal
SEVERE: Failed to start connector [Connector[HTTP/1.1-8973]]
org.apache.catalina.LifecycleException: Failed to start component [Connector[HTTP/1.1-8973]]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
        at org.apache.catalina.core.StandardService.startInternal(StandardService.java:459)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:732)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.startup.Tomcat.start(Tomcat.java:335)
        at com.ul.Bootstrap.main(Bootstrap.java:15)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [org.apache.catalina.connector.MapperListener@768debd]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
        at org.apache.catalina.connector.Connector.startInternal(Connector.java:1022)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        ... 6 more
Caused by: java.lang.NullPointerException
        at org.apache.catalina.connector.MapperListener.findDefaultHost(MapperListener.java:252)
        at org.apache.catalina.connector.MapperListener.startInternal(MapperListener.java:104)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        ... 8 more

X: null
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


WebSockets problem Re: NullPointerException in MapperListener; Tomcat#start() does not create a Container?

Posted by Nick Williams <ni...@nicholaswilliams.net>.
On Mar 14, 2013, at 4:25 PM, Konstantin Kolinko wrote:

> 2013/3/15 Nick Williams <ni...@nicholaswilliams.net>:
>> 
>> On Mar 14, 2013, at 2:56 PM, Nick Williams wrote:
>> 
>>> Using a variety of tutorials I found online and the documentation for o.a.c.startup.Tomcat, I created the following main method to start up an embedded Tomcat. I'm using 7.0.37 Tomcat JARs.
>>> 
>>>   public static void main(String... arguments) throws Exception
>>>   {
>>>       Tomcat tomcat = new Tomcat();
>>>       tomcat.setBaseDir(".basedir");
>>>       tomcat.setPort(8973);
>>>       tomcat.enableNaming();
>>>       tomcat.init();
>>>       tomcat.start();
>>> 
>>>       System.out.println("X: " + tomcat.getConnector().getService().getContainer());
>>> 
>>>       tomcat.getServer().await();
>>>   }
>>> 
>>> The System.out.println is for debugging purposes, because I'm getting a NullPointerException. Obviously I'm doing something wrong, because about an hour of Googling turned up precisely zero results of anyone who's getting a NullPointerException in MapperListener#findDefaultHost. For some reason, it looks like a Container is never created. What gives? Here's the full output of running the JAR file:
> 
> 
> Why do you expect that default "Host" exist, if you have not created
> one, nor asked for one, nor deployed a web application,  all of them
> auto-create it if it is missing.
> 
> There may be different implementations of a Host. It needs
> configuration (name). Thus initially there is none created.
> 
> Calling tomcat.getHost() should be enough.
> 
> Note, that Tomcat needs a default web application (aka context with
> path "", aka ROOT) for certain features (error reporting) to work
> properly.

I understand what I was doing wrong, now. I was trying to take any web application errors out of the picture so that I could solve any other problems I was having first. I didn't realize that you couldn't start normally if you didn't have any applications to deploy, so I was actually causing more problems by removing the adding of my web application.

I don't need tomcat.getService().setContainer(tomcat.getEngine()) anymore now that I'm calling addWebapp on a proper web application.

Mark, my application successfully deploys to the ROOT context in the embedded Tomcat and a test servlet (/healthCheck) starts up properly and requests to it resolve properly. However, my WebSocket endpoints were not working. I was getting 404 errors for any requests to them. It tracked it down to missing service providers. The following files are missing from the org.apache.tomcat.embed:tomcat-embed-core artifact:

META-INF/services/javax.servlet.ServletContainerInitializer
META-INF/services/javax.websocket.ContainerProvider
META-INF/services/javax.websocket.server.ServerContainerProvider
META-INF/services/javax.websocket.server.ServerEndpointConfig.Configurator

When I added those to my uber-jar embedded JAR file, WebSockets started working properly. So, looks like something about the way that artifact is built is omitting those files accidentally. I can file a bug about this if you need me to.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: NullPointerException in MapperListener; Tomcat#start() does not create a Container?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Konstantin,

On 3/14/13 5:25 PM, Konstantin Kolinko wrote:
> 2013/3/15 Nick Williams <ni...@nicholaswilliams.net>:
> 
>> I resolved the NullPointerException by calling 
>> tomcat.getService().setContainer(tomcat.getEngine()) between
>> init() and start(). Everything is working fine now, and I can go
>> to http://localhost:8973/MyServlet and it's working great, but I
>> still suspect I'm doing something wrong, since no documentation
>> or tutorials anywhere mention needing to do that and it seems
>> that the container should automatically be set on the service...
> 
> There are a number of working examples in the testsuite.

+1

The Tomcat class is used everywhere in the test suites. They even run
properly ;)

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEAREIAAYFAlFDd0QACgkQ9CaO5/Lv0PAbkgCeMiVWMr2rHTmyKI+kfXQIoUDU
SGIAnA4sBwbGoPzdwIQ62oF4XxLSQrs8
=0saJ
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: NullPointerException in MapperListener; Tomcat#start() does not create a Container?

Posted by Konstantin Kolinko <kn...@gmail.com>.
2013/3/15 Nick Williams <ni...@nicholaswilliams.net>:
>
> On Mar 14, 2013, at 2:56 PM, Nick Williams wrote:
>
>> Using a variety of tutorials I found online and the documentation for o.a.c.startup.Tomcat, I created the following main method to start up an embedded Tomcat. I'm using 7.0.37 Tomcat JARs.
>>
>>    public static void main(String... arguments) throws Exception
>>    {
>>        Tomcat tomcat = new Tomcat();
>>        tomcat.setBaseDir(".basedir");
>>        tomcat.setPort(8973);
>>        tomcat.enableNaming();
>>        tomcat.init();
>>        tomcat.start();
>>
>>        System.out.println("X: " + tomcat.getConnector().getService().getContainer());
>>
>>        tomcat.getServer().await();
>>    }
>>
>> The System.out.println is for debugging purposes, because I'm getting a NullPointerException. Obviously I'm doing something wrong, because about an hour of Googling turned up precisely zero results of anyone who's getting a NullPointerException in MapperListener#findDefaultHost. For some reason, it looks like a Container is never created. What gives? Here's the full output of running the JAR file:


Why do you expect that default "Host" exist, if you have not created
one, nor asked for one, nor deployed a web application,  all of them
auto-create it if it is missing.

There may be different implementations of a Host. It needs
configuration (name). Thus initially there is none created.

Calling tomcat.getHost() should be enough.

Note, that Tomcat needs a default web application (aka context with
path "", aka ROOT) for certain features (error reporting) to work
properly.

>>
>> Mar 14, 2013 2:39:04 PM org.apache.coyote.AbstractProtocol init
>> INFO: Initializing ProtocolHandler ["http-bio-8973"]
>> Mar 14, 2013 2:39:04 PM org.apache.catalina.core.StandardService startInternal
>> INFO: Starting service Tomcat
>> Mar 14, 2013 2:39:04 PM org.apache.coyote.AbstractProtocol start
>> INFO: Starting ProtocolHandler ["http-bio-8973"]
>> Mar 14, 2013 2:39:04 PM org.apache.catalina.core.StandardService startInternal
>> SEVERE: Failed to start connector [Connector[HTTP/1.1-8973]]
>> org.apache.catalina.LifecycleException: Failed to start component [Connector[HTTP/1.1-8973]]
>>        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
>>        at org.apache.catalina.core.StandardService.startInternal(StandardService.java:459)
>>        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
>>        at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:732)
>>        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
>>        at org.apache.catalina.startup.Tomcat.start(Tomcat.java:335)
>>        at com.ul.Bootstrap.main(Bootstrap.java:15)
>> Caused by: org.apache.catalina.LifecycleException: Failed to start component [org.apache.catalina.connector.MapperListener@768debd]
>>        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
>>        at org.apache.catalina.connector.Connector.startInternal(Connector.java:1022)
>>        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
>>        ... 6 more
>> Caused by: java.lang.NullPointerException
>>        at org.apache.catalina.connector.MapperListener.findDefaultHost(MapperListener.java:252)
>>        at org.apache.catalina.connector.MapperListener.startInternal(MapperListener.java:104)
>>        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
>>        ... 8 more
>>
>> X: null
>
> I resolved the NullPointerException by calling tomcat.getService().setContainer(tomcat.getEngine()) between init() and start(). Everything is working fine now, and I can go to http://localhost:8973/MyServlet and it's working great, but I still suspect I'm doing something wrong, since no documentation or tutorials anywhere mention needing to do that and it seems that the container should automatically be set on the service...

There are a number of working examples in the testsuite.

Best regards,
Konstantin Kolinko

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: NullPointerException in MapperListener; Tomcat#start() does not create a Container?

Posted by Nick Williams <ni...@nicholaswilliams.net>.
On Mar 14, 2013, at 2:56 PM, Nick Williams wrote:

> Using a variety of tutorials I found online and the documentation for o.a.c.startup.Tomcat, I created the following main method to start up an embedded Tomcat. I'm using 7.0.37 Tomcat JARs.
> 
>    public static void main(String... arguments) throws Exception
>    {
>        Tomcat tomcat = new Tomcat();
>        tomcat.setBaseDir(".basedir");
>        tomcat.setPort(8973);
>        tomcat.enableNaming();
>        tomcat.init();
>        tomcat.start();
> 
>        System.out.println("X: " + tomcat.getConnector().getService().getContainer());
> 
>        tomcat.getServer().await();
>    }
> 
> The System.out.println is for debugging purposes, because I'm getting a NullPointerException. Obviously I'm doing something wrong, because about an hour of Googling turned up precisely zero results of anyone who's getting a NullPointerException in MapperListener#findDefaultHost. For some reason, it looks like a Container is never created. What gives? Here's the full output of running the JAR file:
> 
> Mar 14, 2013 2:39:04 PM org.apache.coyote.AbstractProtocol init
> INFO: Initializing ProtocolHandler ["http-bio-8973"]
> Mar 14, 2013 2:39:04 PM org.apache.catalina.core.StandardService startInternal
> INFO: Starting service Tomcat
> Mar 14, 2013 2:39:04 PM org.apache.coyote.AbstractProtocol start
> INFO: Starting ProtocolHandler ["http-bio-8973"]
> Mar 14, 2013 2:39:04 PM org.apache.catalina.core.StandardService startInternal
> SEVERE: Failed to start connector [Connector[HTTP/1.1-8973]]
> org.apache.catalina.LifecycleException: Failed to start component [Connector[HTTP/1.1-8973]]
>        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
>        at org.apache.catalina.core.StandardService.startInternal(StandardService.java:459)
>        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
>        at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:732)
>        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
>        at org.apache.catalina.startup.Tomcat.start(Tomcat.java:335)
>        at com.ul.Bootstrap.main(Bootstrap.java:15)
> Caused by: org.apache.catalina.LifecycleException: Failed to start component [org.apache.catalina.connector.MapperListener@768debd]
>        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
>        at org.apache.catalina.connector.Connector.startInternal(Connector.java:1022)
>        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
>        ... 6 more
> Caused by: java.lang.NullPointerException
>        at org.apache.catalina.connector.MapperListener.findDefaultHost(MapperListener.java:252)
>        at org.apache.catalina.connector.MapperListener.startInternal(MapperListener.java:104)
>        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
>        ... 8 more
> 
> X: null

I resolved the NullPointerException by calling tomcat.getService().setContainer(tomcat.getEngine()) between init() and start(). Everything is working fine now, and I can go to http://localhost:8973/MyServlet and it's working great, but I still suspect I'm doing something wrong, since no documentation or tutorials anywhere mention needing to do that and it seems that the container should automatically be set on the service...

I'm open to suggestions if anybody has any, but at least it's working now.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org