Bug 58629

Summary: Container is not initialized while starting tomcat in embedded mode.
Product: Tomcat 9 Reporter: Sagar Gandhi <gandhi.sagar>
Component: CatalinaAssignee: Tomcat Developers Mailing List <dev>
Status: RESOLVED FIXED    
Severity: normal CC: 271759231
Priority: P2    
Version: 9.0.0.M1   
Target Milestone: -----   
Hardware: PC   
OS: All   
Attachments: Patch for the solution.

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.