MyFaces hellow world application give NullPointerException error
facelets, java, jsf
Solution
Your concrete problem is essentially caused by deploying a Facelets 1.x flavored web application to a Facelets 2.x flavored target container. This version incompatibility is causing the NPE in Facelets 1.x's internal code because it didn't expect to run in a Facelets 2.x environment. The `com.sun.facelets.*` lines in the stack trace confirm that Facelets 1.x is being used (if it were Facelets 2.x, as it should be, then `com.sun.faces.facelets.*` package would instead be used).
Throw away that legacy J2EE 1.4 / JSF 1.x / Facelets 1.0 flavored project. They are completely outdated and doing no good on Java EE 6/7 environments. Concentrate on finding JSF 2.x projects, resources, tutorials and books. Our JSF wiki page is a good starting point.
Problem
I am a beginner with Java (as in 2 weeks of reading), and I would like to learn more by using a project created with Eclipse IDE. What I am using: - Java JDK 1.7 - Eclipse IDE Juno - Maven latest version - JBoss (7.1.1, 6.2 EAP, WidlFly) - all have the same behaviour - A maven project created with Maven archetype: myfaces-archetype-helloworld-facelets. When trying to run, I receive a java.lang.NullPointerException in page. The log from JBoss is showing: ``` 12:25:58,890 INFO [org.apache.catalina.core] (ServerService Thread Pool -- 33) JBWEB001093: The listener org.apache.myfaces.webapp.StartupServletContextListener is already configured for this context, the duplicate definition has been ignored 12:25:58,907 INFO [org.apache.myfaces.shared_impl.config.MyfacesConfig] (ServerService Thread Pool -- 33) No context init parameter 'org.apache.myfaces.RENDER_VIEWSTATE_ID' found, using default value true 12:25:58,907 INFO [org.apache.myfaces.shared_impl.config.MyfacesConfig] (ServerService Thread Pool -- 33) No context init parameter 'org.apache.myfaces.STRICT_XHTML_LINKS' found, using default value true 12:25:58,907 INFO [org.apache.myfaces.shared_impl.config.MyfacesConfig] (ServerService Thread Pool -- 33) No context init parameter 'org.apache.myfaces.CONFIG_REFRESH_PERIOD' found, using default value 2 12:25:58,907 INFO [org.apache.myfaces.shared_impl.config.MyfacesConfig] (ServerService Thread Pool -- 33) No context init parameter 'org.apache.myfaces.VIEWSTATE_JAVASCRIPT' found, using default value false 12:25:58,908 INFO [org.apache.myfaces.shared_impl.config.MyfacesConfig] (ServerService Thread Pool -- 33) Starting up Tomahawk on the MyFaces-JSF-Implementation 12:25:58,919 INFO [org.apache.myfaces.config.FacesConfigurator] (ServerService Thread Pool -- 33) Reading standard config META-INF/standard-faces-config.xml 12:25:58,972 INFO [org.apache.myfaces.config.FacesConfigurator] (ServerService Thread Pool -- 33) Reading config vfs:/D:/JBoss/jboss-eap-6.2/standalone/deployments/test.war/WEB-INF/lib/jsf-facelets-1.1.11.jar/META-INF/faces-config.xml 12:25:58,976 INFO [org.apache.myfaces.config.FacesConfigurator] (ServerService Thread Pool -- 33) Reading config vfs:/D:/JBoss/jboss-eap-6.2/standalone/deployments/test.war/WEB-INF/lib/tomahawk-1.1.5.jar/META-INF/faces-config.xml 12:25:58,997 INFO [org.apache.myfaces.config.FacesConfigurator] (ServerService Thread Pool -- 33) Reading config /WEB-INF/examples-config.xml 12:25:59,005 INFO [org.apache.myfaces.config.FacesConfigurator] (ServerService Thread Pool -- 33) MyFaces-package : myfaces-api not found. 12:25:59,005 INFO [org.apache.myfaces.config.FacesConfigurator] (ServerService Thread Pool -- 33) MyFaces-package : myfaces-impl not found. 12:25:59,005 INFO [org.apache.myfaces.config.FacesConfigurator] (ServerService Thread Pool -- 33) MyFaces-package : tomahawk-sandbox not found. 12:25:59,005 INFO [org.apache.myfaces.config.FacesConfigurator] (ServerService Thread Pool -- 33) MyFaces-package : tomahawk not found. 12:25:59,012 WARN [org.apache.myfaces.shared_impl.util.LocaleUtils] (ServerService Thread Pool -- 33) Locale name in faces-config.xml null or empty, setting locale to default locale : en_US 12:25:59,137 INFO [org.apache.myfaces.config.FacesConfigurator] (ServerService Thread Pool -- 33) Serialization provider : class org.apache.myfaces.shared_impl.util.serial.DefaultSerialFactory 12:25:59,144 INFO [org.apache.myfaces.webapp.AbstractFacesInitializer] (ServerService Thread Pool -- 33) ServletContext 'D:\JBoss\jboss-eap-6.2\standalone\deployments\test.war\' initialized. 12:25:59,201 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018565: Replaced deployment "test.war" with deployment "test.war" 12:26:09,185 SEVERE [facelets.compiler] (http-localhost/127.0.0.1:8080-1) Missing Built-in Tag Libraries! Make sure they are included within the META-INF directory of Facelets' Jar 12:26:09,207 SEVERE [facelets.viewhandler] (http-localhost/127.0.0.1:8080-1) Error Rendering View[/helloWorld.xhtml]: java.lang.NullPointerException Below is the entire stacktrace: java.lang.NullPointerException at com.sun.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:49) at com.sun.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:25) at com.sun.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:95) at com.sun.facelets.FaceletViewHandler.buildView(FaceletViewHandler.java:510) at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:553) at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:85) at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:239) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:191) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:147) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:246) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149) at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:336) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:920) at java.lang.Thread.run(Thread.java:744) ``` From my research, a potential problem would be that this archetype project is trying to override the existing facelets implementation that already exists in JBoss. This was a problem in JBoss 7.1, but the same error is also in 7.2 (6.2 EAP). PS the project is using: WAR_BUNDLES_JSF_IMPL. The pom file is below. ``` <?xml version="1.0"?> <!-- * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. --> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <description>MyProject web.xml</description> <context-param> <param-name> org.jboss.jbossfaces.WAR_BUNDLES_JSF_IMPL </param-name> <param-value>true</param-value> </context-param> <!-- Configure tomahawk taglib --> <context-param> <param-name>facelets.LIBRARIES</param-name> <param-value>/WEB-INF/tomahawk.taglib.xml</param-value> </context-param> <!-- Use Documents Saved as *.xhtml --> <context-param> <param-name>javax.faces.DEFAULT_SUFFIX</param-name> <param-value>.xhtml</param-value> </context-param> <!-- Special Debug Output for Development --> <context-param> <param-name>facelets.DEVELOPMENT</param-name> <param-value>true</param-value> </context-param> <context-param> <description> Comma separated list of URIs of (additional) faces config files. (e.g. /WEB-INF/my-config.xml) See JSF 1.0 PRD2, 10.3.2 Attention: You do not need to put /WEB-INF/faces-config.xml in here. </description> <param-name>javax.faces.CONFIG_FILES</param-name> <param-value>/WEB-INF/examples-config.xml</param-value> </context-param> <context-param> <description> State saving method: "client" or "server" (= default) See JSF Specification 2.5.3 </description> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>client</param-value> </context-param> <context-param> <description> Only applicable if state saving method is "server" (= default). Defines the amount (default = 20) of the latest views are stored in session. </description> <param-name> org.apache.myfaces.NUMBER_OF_VIEWS_IN_SESSION </param-name> <param-value>20</param-value> </context-param> <context-param> <description> Only applicable if state saving method is "server" (= default). If true (default) the state will be serialized to a byte stream before it is written to the session. If false the state will not be serialized to a byte stream. </description> <param-name> org.apache.myfaces.SERIALIZE_STATE_IN_SESSION </param-name> <param-value>true</param-value> </context-param> <context-param> <description> Only applicable if state saving method is "server" (= default) and if org.apache.myfaces.SERIALIZE_STATE_IN_SESSION is true (= default) If true (default) the serialized state will be compressed before it is written to the session. If false the state will not be compressed. </description> <param-name> org.apache.myfaces.COMPRESS_STATE_IN_SESSION </param-name> <param-value>true</param-value> </context-param> <context-param> <description> This parameter tells MyFaces if javascript code should be allowed in the rendered HTML output. If javascript is allowed, command_link anchors will have javascript code that submits the corresponding form. If javascript is not allowed, the state saving info and nested parameters will be added as url parameters. Default: "true" </description> <param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name> <param-value>true</param-value> </context-param> <context-param> <param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name> <param-value>false</param-value> </context-param> <context-param> <description> If true, rendered HTML code will be formatted, so that it is "human readable". i.e. additional line separators and whitespace will be written, that do not influence the HTML code. Default: "true" </description> <param-name>org.apache.myfaces.PRETTY_HTML</param-name> <param-value>true</param-value> </context-param> <context-param> <description> If true, a javascript function will be rendered that is able to restore the former vertical scroll on every request. Convenient feature if you have pages with long lists and you do not want the browser page to always jump to the top if you trigger a link or button action that stays on the same page. Default: "false" </description> <param-name>org.apache.myfaces.AUTO_SCROLL</param-name> <param-value>true</param-value> </context-param> <context-param> <description> Used for encrypting view state. Only relevant for client side state saving. See MyFaces wiki/web site documentation for instructions on how to configure an application for diffenent encryption strengths. </description> <param-name>org.apache.myfaces.SECRET</param-name> <param-value>NzY1NDMyMTA=</param-value> </context-param> <context-param> <description> Validate managed beans, navigation rules and ensure that forms are not nested. </description> <param-name>org.apache.myfaces.VALIDATE</param-name> <param-value>true</param-value> </context-param> <context-param> <description> Treat readonly same as if disabled attribute was set for select elements. </description> <param-name> org.apache.myfaces.READONLY_AS_DISABLED_FOR_SELECTS </param-name> <param-value>true</param-value> </context-param> <context-param> <description> Use the defined class as the class which will be called when a resource is added to the ExtensionFilter handling. Using StreamingAddResource here helps with performance. If you want to add custom components and want to use the ExtensionFilter, you need to provide your custom implementation here. </description> <param-name>org.apache.myfaces.ADD_RESOURCE_CLASS</param-name> <param-value> org.apache.myfaces.renderkit.html.util.DefaultAddResource </param-value> </context-param> <context-param> <description> Virtual path in the URL which triggers loading of resources for the MyFaces extended components in the ExtensionFilter. </description> <param-name> org.apache.myfaces.RESOURCE_VIRTUAL_PATH </param-name> <param-value>/faces/myFacesExtensionResource</param-value> </context-param> <context-param> <description> Check if the extensions-filter has been properly configured. </description> <param-name> org.apache.myfaces.CHECK_EXTENSIONS_FILTER </param-name> <param-value>true</param-value> </context-param> <context-param> <description> Define partial state saving as true/false. </description> <param-name>javax.faces.PARTIAL_STATE_SAVING_METHOD</param-name> <param-value>false</param-value> </context-param> <!-- Extensions Filter --> <filter> <filter-name>extensionsFilter</filter-name> <filter-class> org.apache.myfaces.webapp.filter.ExtensionsFilter </filter-class> <init-param> <description> Set the size limit for uploaded files. Format: 10 - 10 bytes 10k - 10 KB 10m - 10 MB 1g - 1 GB </description> <param-name>uploadMaxFileSize</param-name> <param-value>100m</param-value> </init-param> <init-param> <description> Set the threshold size - files below this limit are stored in memory, files above this limit are stored on disk. Format: 10 - 10 bytes 10k - 10 KB 10m - 10 MB 1g - 1 GB </description> <param-name>uploadThresholdSize</param-name> <param-value>100k</param-value> </init-param> </filter> <filter-mapping> <filter-name>extensionsFilter</filter-name> <url-pattern>*.jsf</url-pattern> </filter-mapping> <filter-mapping> <filter-name>extensionsFilter</filter-name> <url-pattern>/faces/*</url-pattern> </filter-mapping> <!-- Listener, to allow Jetty serving MyFaces apps --> <listener> <listener-class> org.apache.myfaces.webapp.StartupServletContextListener </listener-class> </listener> <!-- Faces Servlet --> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- Faces Servlet Mapping --> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.jsf</url-pattern> </servlet-mapping> <!-- Welcome files --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app> ``` Edited added page code The code of the page is very simple. I think is about deployment, not the code (see below): ``` <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> <head> <title>Hello World</title> </head> <body> <ui:composition template="/template.xhtml"> <ui:define name="body"> <h:form id="form"> </h:form> </ui:define> </ui:composition> </body> </html> ``` I am also adding the web.xml, just in case. ``` <?xml version="1.0"?> <!-- * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. --> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <description>MyProject web.xml</description> <context-param> <param-name> org.jboss.jbossfaces.WAR_BUNDLES_JSF_IMPL </param-name> <param-value>true</param-value> </context-param> <!-- Configure tomahawk taglib --> <context-param> <param-name>facelets.LIBRARIES</param-name> <param-value>/WEB-INF/tomahawk.taglib.xml</param-value> </context-param> <!-- Use Documents Saved as *.xhtml --> <context-param> <param-name>javax.faces.DEFAULT_SUFFIX</param-name> <param-value>.xhtml</param-value> </context-param> <!-- Special Debug Output for Development --> <context-param> <param-name>facelets.DEVELOPMENT</param-name> <param-value>true</param-value> </context-param> <context-param> <description> Comma separated list of URIs of (additional) faces config files. (e.g. /WEB-INF/my-config.xml) See JSF 1.0 PRD2, 10.3.2 Attention: You do not need to put /WEB-INF/faces-config.xml in here. </description> <param-name>javax.faces.CONFIG_FILES</param-name> <param-value>/WEB-INF/examples-config.xml</param-value> </context-param> <context-param> <description> State saving method: "client" or "server" (= default) See JSF Specification 2.5.3 </description> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>client</param-value> </context-param> <context-param> <description> Only applicable if state saving method is "server" (= default). Defines the amount (default = 20) of the latest views are stored in session. </description> <param-name> org.apache.myfaces.NUMBER_OF_VIEWS_IN_SESSION </param-name> <param-value>20</param-value> </context-param> <context-param> <description> Only applicable if state saving method is "server" (= default). If true (default) the state will be serialized to a byte stream before it is written to the session. If false the state will not be serialized to a byte stream. </description> <param-name> org.apache.myfaces.SERIALIZE_STATE_IN_SESSION </param-name> <param-value>true</param-value> </context-param> <context-param> <description> Only applicable if state saving method is "server" (= default) and if org.apache.myfaces.SERIALIZE_STATE_IN_SESSION is true (= default) If true (default) the serialized state will be compressed before it is written to the session. If false the state will not be compressed. </description> <param-name> org.apache.myfaces.COMPRESS_STATE_IN_SESSION </param-name> <param-value>true</param-value> </context-param> <context-param> <description> This parameter tells MyFaces if javascript code should be allowed in the rendered HTML output. If javascript is allowed, command_link anchors will have javascript code that submits the corresponding form. If javascript is not allowed, the state saving info and nested parameters will be added as url parameters. Default: "true" </description> <param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name> <param-value>true</param-value> </context-param> <context-param> <param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name> <param-value>false</param-value> </context-param> <context-param> <description> If true, rendered HTML code will be formatted, so that it is "human readable". i.e. additional line separators and whitespace will be written, that do not influence the HTML code. Default: "true" </description> <param-name>org.apache.myfaces.PRETTY_HTML</param-name> <param-value>true</param-value> </context-param> <context-param> <description> If true, a javascript function will be rendered that is able to restore the former vertical scroll on every request. Convenient feature if you have pages with long lists and you do not want the browser page to always jump to the top if you trigger a link or button action that stays on the same page. Default: "false" </description> <param-name>org.apache.myfaces.AUTO_SCROLL</param-name> <param-value>true</param-value> </context-param> <context-param> <description> Used for encrypting view state. Only relevant for client side state saving. See MyFaces wiki/web site documentation for instructions on how to configure an application for diffenent encryption strengths. </description> <param-name>org.apache.myfaces.SECRET</param-name> <param-value>NzY1NDMyMTA=</param-value> </context-param> <context-param> <description> Validate managed beans, navigation rules and ensure that forms are not nested. </description> <param-name>org.apache.myfaces.VALIDATE</param-name> <param-value>true</param-value> </context-param> <context-param> <description> Treat readonly same as if disabled attribute was set for select elements. </description> <param-name> org.apache.myfaces.READONLY_AS_DISABLED_FOR_SELECTS </param-name> <param-value>true</param-value> </context-param> <context-param> <description> Use the defined class as the class which will be called when a resource is added to the ExtensionFilter handling. Using StreamingAddResource here helps with performance. If you want to add custom components and want to use the ExtensionFilter, you need to provide your custom implementation here. </description> <param-name>org.apache.myfaces.ADD_RESOURCE_CLASS</param-name> <param-value> org.apache.myfaces.renderkit.html.util.DefaultAddResource </param-value> </context-param> <context-param> <description> Virtual path in the URL which triggers loading of resources for the MyFaces extended components in the ExtensionFilter. </description> <param-name> org.apache.myfaces.RESOURCE_VIRTUAL_PATH </param-name> <param-value>/faces/myFacesExtensionResource</param-value> </context-param> <context-param> <description> Check if the extensions-filter has been properly configured. </description> <param-name> org.apache.myfaces.CHECK_EXTENSIONS_FILTER </param-name> <param-value>true</param-value> </context-param> <context-param> <description> Define partial state saving as true/false. </description> <param-name>javax.faces.PARTIAL_STATE_SAVING_METHOD</param-name> <param-value>false</param-value> </context-param> <!-- Extensions Filter --> <filter> <filter-name>extensionsFilter</filter-name> <filter-class> org.apache.myfaces.webapp.filter.ExtensionsFilter </filter-class> <init-param> <description> Set the size limit for uploaded files. Format: 10 - 10 bytes 10k - 10 KB 10m - 10 MB 1g - 1 GB </description> <param-name>uploadMaxFileSize</param-name> <param-value>100m</param-value> </init-param> <init-param> <description> Set the threshold size - files below this limit are stored in memory, files above this limit are stored on disk. Format: 10 - 10 bytes 10k - 10 KB 10m - 10 MB 1g - 1 GB </description> <param-name>uploadThresholdSize</param-name> <param-value>100k</param-value> </init-param> </filter> <filter-mapping> <filter-name>extensionsFilter</filter-name> <url-pattern>*.jsf</url-pattern> </filter-mapping> <filter-mapping> <filter-name>extensionsFilter</filter-name> <url-pattern>/faces/*</url-pattern> </filter-mapping> <!-- Listener, to allow Jetty serving MyFaces apps --> <listener> <listener-class> org.apache.myfaces.webapp.StartupServletContextListener </listener-class> </listener> <!-- Faces Servlet --> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- Faces Servlet Mapping --> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.jsf</url-pattern> </servlet-mapping> <!-- Welcome files --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app> ```