2013/3/15 Nick Williams <nicho...@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-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to