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)
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.