Bug 58629 - Container is not initialized while starting tomcat in embedded mode.
Summary: Container is not initialized while starting tomcat in embedded mode.
Status: RESOLVED FIXED
Alias: None
Product: Tomcat 9
Classification: Unclassified
Component: Catalina (show other bugs)
Version: 9.0.0.M1
Hardware: PC All
: P2 normal (vote)
Target Milestone: -----
Assignee: Tomcat Developers Mailing List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-11-20 09:08 UTC by Sagar Gandhi
Modified: 2016-10-02 03:11 UTC (History)
1 user (show)



Attachments
Patch for the solution. (341 bytes, patch)
2015-11-20 09:08 UTC, Sagar Gandhi
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Sagar Gandhi 2015-11-20 09:08:12 UTC
Created attachment 33284 [details]
Patch for the solution.

For earliar versions, following code works. But for tomcat 9 , It is giving null pointer exception while starting the server.

	Tomcat tomcat = new Tomcat();
	tomcat.setHostname("localhost");
	tomcat.setPort(8080);
	tomcat.setBaseDir("D:/softwares/servers/STUDY_TOMCAT");
	tomcat.start();


Exception in thread "main" org.apache.catalina.LifecycleException: Failed to start component [StandardServer[-1]]
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:159)
	at org.apache.catalina.startup.Tomcat.start(Tomcat.java:345)
	at com.gandhisagar.test.TomcatStart.main(TomcatStart.java:13)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardService[Tomcat]]
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:159)
	at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:769)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:155)
	... 2 more
Caused by: org.apache.catalina.LifecycleException: Failed to start component [org.apache.catalina.mapper.MapperListener@685f4c2e]
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:159)
	at org.apache.catalina.core.StandardService.startInternal(StandardService.java:451)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:155)
	... 4 more
Caused by: java.lang.NullPointerException
	at org.apache.catalina.mapper.MapperListener.findDefaultHost(MapperListener.java:261)
	at org.apache.catalina.mapper.MapperListener.startInternal(MapperListener.java:102)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:155)
	... 6 more


Solution:

While debugging the issue we found that , Container is not getting initialized properly. So we need to call corg.apache.catalina.startup.Tomcat.getEngine() while starting the server in embedded mode. So that same code will work with Tomcat 9.0

(Attaching the patch with the bug)
Comment 1 Mark Thomas 2015-11-20 20:39:31 UTC
This issue doesn't appear to be unique to 9.0.x

I've fixed it in 9.0.x for 9.0.0.M2 onwards and in 8.0.x for 8.0.30 onwards.

I took a different approach to fixing the NPE. The MapperListener now skips registration if the engine is null.