0

using jsf causes a NullpointerException:

    WARNING: StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
    at javax.faces.webapp.UIComponentClassicTagBase.setJspId(UIComponentClassicTagBase.java:1858)
    at org.apache.jsp.jsf.list_jsp._jspx_meth_h_form_0(list_jsp.java:112)
    at org.apache.jsp.jsf.list_jsp._jspService(list_jsp.java:86)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)

In my lib folder of my glassfish-server and of my dynamic web-project are the following jars:

  • standard-1.1.2.jar
  • jstl-jstl-1.2.jar
  • jsp-api-2.1
  • jsf-impl-2.1.7.jar
  • jsf-api-2.2.0-m03.jar

My Bean:

@Stateless
@Named 
public class StatelessBean {

    public int getNumberOfMovies() {
        return 42;
    }

    public int getNumberOfPersons() {
        return 42;
    }
}

My jsf:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="../css/basic.css" type="text/css" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Liste</title>
</head>
<body>
    <h:form>
        <h1>Stateless</h1>
        <h:outputText value="Anzahl der Filme: #{StatelessBean.getNumberOfMovies()}" />
        <h:outputText value="Anzahl der Personen: #{StatelessBean.getNumberOfPersons()}" />
    </h:form>   
</body>
</html>

The awnsers provided here: stackoverflow didnt help.

Dont know how to resolv this.

Community
  • 1
  • 1
Mulgard
  • 9,877
  • 34
  • 129
  • 232
  • I've never seen bean names starting by a caps inside a view... Also you should use `xmlns` instead of `taglib`. – Alexandre Lavoie May 31 '13 at 17:25
  • the bean starting with a caps was a try... without caps its also nullpointerexception. and why xmlns? – Mulgard May 31 '13 at 17:34
  • 2
    Please read JSF 2.0 books/tutorials/resources if you want to learn JSF 2.0 and not JSF 1.x ones. You're using JSP which is deprecated since JSF 2.0 more than 3 years ago. This indicates that you were reading completely outdated books/tutorials/resources. Also, the presence of `standard-1.1.2.jar` and `jsp-api-2.1.jar` files in your `/WEB-INF/lib` is scary. Your JSF API and impl JARs are also incompatibile in versions. Why exactly did you do that? Reading bad tutorials? Shooting in the dark? Those files would only cause portability trouble. – BalusC May 31 '13 at 18:50

2 Answers2

4

You should change your view code like this :

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <link href="css/basic.css" type="text/css" rel="stylesheet">
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Liste</title>
    </h:head>
    <h:body>
        <h:form>
            <h1>Stateless</h1>
            <h:outputText value="Anzahl der Filme: #{statelessBean.getNumberOfMovies()}" />
            <h:outputText value="Anzahl der Personen: #{statelessBean.getNumberOfPersons()}" />
        </h:form>   
    </h:body>
</html>

This is the way in JSF do declare taglibs.

You will also need to change some configurations in your web.xml and rename your file to yourfile.xhtml :

<context-param>
    <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
    <param-value>.xhtml</param-value>
</context-param>

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>

<!-- Use prefix mapping for Facelets pages, e.g. http://localhost:8080/webapp/yourfile.jspx -->
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jspx</url-pattern>
</servlet-mapping>

To use your bean in EL expressions, you need to change it like this :

@ManagedBean
@RequestScoped
public class StatelessBean {

    public int getNumberOfMovies() {
        return 42;
    }

    public int getNumberOfPersons() {
        return 42;
    }
}

More info :

Alexandre Lavoie
  • 8,711
  • 3
  • 31
  • 72
  • Doesnt jsf with jsp use taglibs ? – Adarsh May 31 '13 at 17:51
  • But that is also not working: org.apache.jasper.JasperException: /jsf/list.jsp(25,43) PWC6228: #{...} not allowed in a template text body. – Mulgard May 31 '13 at 17:53
  • Okay with this i get at least an output. But -> no css is working -> my "statelessBean.getNumberOfMovies()" isnt working (no output) – Mulgard May 31 '13 at 18:09
  • See edited, I've also modified css path, should work now. For this you could take a look at `h:outputStylesheet` on Google, this is the way! – Alexandre Lavoie May 31 '13 at 18:15
  • http://www.mkyong.com/jsf2/how-to-include-cascading-style-sheets-css-in-jsf/ her i read that there should be generated html code. hmmm.. i dont get css working. i think its enough for today. thank you :) – Mulgard May 31 '13 at 18:31
  • @MarcTigges you should put your css in /resources/css/basic.css and use `` that should work. – Alexandre Lavoie May 31 '13 at 18:43
  • yeah i read this already and i did it. it isnt working. i will find out somehow why it isnt working. thank you – Mulgard May 31 '13 at 18:47
  • Why did you accept the answer if it didn't work for you? This answer didn't cover all problematic aspects either. – BalusC May 31 '13 at 18:50
  • @BalusC I'm not as expiremented as you but your comments will be appreciated if I could make this answer more precise. For the CSS, this is not directly related to the question but with this pointers this _should_ work properly. – Alexandre Lavoie May 31 '13 at 18:58
  • "This answer didn't cover all problematic aspects either." which not? i accepted cause my original problem was solved -> the nullpointerexception. i just dont get output for my bean and css not working but that wasnt part of my question so i thought its legal to accept – Mulgard May 31 '13 at 19:26
0

Try changing the call to getNumberOfMovies. JSF silently adds the word 'get' and the brackets '()' to the value in your html. The idea is that you write getters and setters and then just name the property and JSF calls the appropriate one.

<h:outputText value="Anzahl der Filme: #{statelessBean.numberOfMovies}" />
        <h:outputText value="Anzahl der Personen: #{statelessBean.numberOfPersons}" />

However, this invokes a propertyNotFoundException. It may also be the cause of your null pointer but I'm not sure.

halfer
  • 19,824
  • 17
  • 99
  • 186