servlet .service()的servlet面临着一个例外:java.lang。NullPointerException JSF

时间:2022-01-07 21:12:12

Is there a fix to the below error. When i click the command button it calls the java script to set the values for topic_body,topic_url and submit the values to a method in server by the commandbutton. I am not sure what was need to be corrected to stop the error.

下面的错误有固定的地方吗?当我单击命令按钮时,它调用java脚本为topic_body、topic_url设置值,并通过commandbutton将值提交到服务器中的方法。我不确定需要纠正什么来阻止错误。

XHTML

XHTML

<h:form prependId="false" id="testForm">
        <h:inputHidden value="#{topicBean.topicVO.body}" id="topic_body">
        </h:inputHidden>
        <h:inputHidden value="#{topicBean.topicVO.videoAudioUrl}" id="topic_vaurl" >
        </h:inputHidden>
        <h:inputHidden value="#{topicBean.topic_guid}" id="topic_guid">
        </h:inputHidden>
                <p:commandButton id="shareeButton" action="#{topicController.createTopic}" onclick="javascript:renderElement();">
                Share
                <f:ajax execute="@form" render="@form"></f:ajax>
                </p:commandButton>
            </h:form>

Javascript

Javascript

function  renderElement(){
           var textarea_content = $('textarea#wall').val();
           $('#topic_body').val(textarea_content);
           if (textarea_content != '') {
               var sitetitle = $('label.title').html();
               if (sitetitle == null) {
                   sitetitle = ' ';
               }
.......
......
..

}

JSF Bean

JSF豆

@ManagedBean(name = "topicBean")
@SessionScoped
public class TopicBean extends BaseMB implements Serializable {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private final static Logger logger = Logger.getLogger(CircleBean.class.toString());
    private TopicVO topicVO = new TopicVO();
    private long topic_guid =0;
    public TopicBean(){
        logger.info("****************************Start TopicBean Constructor****************************************");
        logger.info("****************************End TopicBean Constructor****************************************");

    }

JSF Method

JSF的方法

@ManagedBean(name = "topicController")
@RequestScoped
public class TopicController  extends BaseMB implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private final static Logger logger = Logger
            .getLogger(CircleController.class.toString());
    TopicBean topicBean;


    public void createTopic(){
        logger.info("*******************Inside creatTopic********************");
        topicBean = (TopicBean) FacesUtils.getManagedBean("topicBean");
        ITopicService topicService = (ITopicService) ServiceLocator
                .getService("TOPICSERVICE");
         topicBean.setTopic_guid(topicService.createTopic(topicBean.getTopicVO(), userSession));

        logger.info("*******************Inside creatTopic********************");
    }

ERROR

错误

14:44:20,335 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/emyed-web].[Faces Servlet]] (http--127.0.0.1-8080-3) Servlet.service() for servlet Faces Servlet threw exception: java.lang.NullPointerException
    at com.sun.faces.mgbean.BeanManager$ScopeManager$SessionScopeHandler.handle(BeanManager.java:575) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.mgbean.BeanManager$ScopeManager.pushToScope(BeanManager.java:458) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:410) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.zreflect.emyed.filter.EmyEdAccessFilter.hasLoggedIn(EmyEdAccessFilter.java:113) [emyed-ui-1.0.jar:]
    at com.zreflect.emyed.filter.EmyEdAccessFilter.doFilter(EmyEdAccessFilter.java:84) [emyed-ui-1.0.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:897) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:626) [jbossweb-7.0.13.Final.jar:]
    at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:2039) [jbossweb-7.0.13.Final.jar:]
    at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_25]



enter code here

2 个解决方案

#1


4  

According to the stacktrace,

根据加亮,

java.lang.NullPointerException
    at com.sun.faces.mgbean.BeanManager$ScopeManager$SessionScopeHandler.handle(BeanManager.java:575) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.mgbean.BeanManager$ScopeManager.pushToScope(BeanManager.java:458) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:410) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.zreflect.emyed.filter.EmyEdAccessFilter.hasLoggedIn(EmyEdAccessFilter.java:113) [emyed-ui-1.0.jar:]
    at com.zreflect.emyed.filter.EmyEdAccessFilter.doFilter(EmyEdAccessFilter.java:84) [emyed-ui-1.0.jar:]

you've a Filter class EmyEdAccessFilter which is trying to resolve a JSF-specific EL expression using ValueExpression, most likely with the sole purpose to find a session scoped JSF managed bean or its property which should represent the logged-in user. But this EL resolving failed because there's no means of a FacesContext inside a servlet filter. The FacesContext is supposed to be created by the FacesServlet, but at the moment the filter runs, the FacesServlet hasn't run at all and thus the FacesContext.getCurrentInstance() returns null and thus your code breaks.

您有一个过滤器类EmyEdAccessFilter,它试图使用ValueExpression解析一个特定于JSF的EL表达式,最有可能的目的是找到一个会话范围的JSF托管bean或它的属性,该属性应该表示登录用户。但是这个EL解析失败了,因为servlet筛选器中没有FacesContext。FacesContext应该由FacesServlet创建,但在过滤器运行时,FacesServlet并没有运行,因此FacesContext. getcurrentinstance()返回null,因此代码中断。

All with all, this is not the right way to check a session scoped JSF managed bean in a filter.

总之,在过滤器中检查会话范围的JSF托管bean并不是正确的方法。

Under JSF's covers, session scoped managed beans are stored as attributes of the HttpSession with the managed bean name as session attribute name. You can just access them "the usual way" as session attributes in a servlet filter. E.g.

在JSF的覆盖下,会话范围管理的bean被存储为HttpSession的属性,以托管bean名称作为会话属性名。您可以在servlet筛选器中以“通常的方式”作为会话属性访问它们。如。

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;
    HttpSession session = request.getSession(false);
    UserManager userManager = (session != null) ? (UserManager) session.getAttribute("userManager") : null;

    if (userManager == null || !userManager.isLoggedIn()) {
        response.sendRedirect(request.getContextPath() + "/login.xhtml"); // No logged-in user found, so redirect to login page.
    } else {
        chain.doFilter(req, res); // Logged-in user found, so just continue request.
    }
}

Where UserManager represents your session scoped JSF managed bean.

在这里,UserManager表示您的会话作用域的JSF托管bean。

See also:

#2


0  

Also check @ManagedProperty is correct. For example:

也检查@ManagedProperty是否正确。例如:

@ManagedProperty(value = "#{user_ejb}")
UserEJB userEJB;

Or @Inject

或@ inject

#1


4  

According to the stacktrace,

根据加亮,

java.lang.NullPointerException
    at com.sun.faces.mgbean.BeanManager$ScopeManager$SessionScopeHandler.handle(BeanManager.java:575) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.mgbean.BeanManager$ScopeManager.pushToScope(BeanManager.java:458) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:410) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.zreflect.emyed.filter.EmyEdAccessFilter.hasLoggedIn(EmyEdAccessFilter.java:113) [emyed-ui-1.0.jar:]
    at com.zreflect.emyed.filter.EmyEdAccessFilter.doFilter(EmyEdAccessFilter.java:84) [emyed-ui-1.0.jar:]

you've a Filter class EmyEdAccessFilter which is trying to resolve a JSF-specific EL expression using ValueExpression, most likely with the sole purpose to find a session scoped JSF managed bean or its property which should represent the logged-in user. But this EL resolving failed because there's no means of a FacesContext inside a servlet filter. The FacesContext is supposed to be created by the FacesServlet, but at the moment the filter runs, the FacesServlet hasn't run at all and thus the FacesContext.getCurrentInstance() returns null and thus your code breaks.

您有一个过滤器类EmyEdAccessFilter,它试图使用ValueExpression解析一个特定于JSF的EL表达式,最有可能的目的是找到一个会话范围的JSF托管bean或它的属性,该属性应该表示登录用户。但是这个EL解析失败了,因为servlet筛选器中没有FacesContext。FacesContext应该由FacesServlet创建,但在过滤器运行时,FacesServlet并没有运行,因此FacesContext. getcurrentinstance()返回null,因此代码中断。

All with all, this is not the right way to check a session scoped JSF managed bean in a filter.

总之,在过滤器中检查会话范围的JSF托管bean并不是正确的方法。

Under JSF's covers, session scoped managed beans are stored as attributes of the HttpSession with the managed bean name as session attribute name. You can just access them "the usual way" as session attributes in a servlet filter. E.g.

在JSF的覆盖下,会话范围管理的bean被存储为HttpSession的属性,以托管bean名称作为会话属性名。您可以在servlet筛选器中以“通常的方式”作为会话属性访问它们。如。

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;
    HttpSession session = request.getSession(false);
    UserManager userManager = (session != null) ? (UserManager) session.getAttribute("userManager") : null;

    if (userManager == null || !userManager.isLoggedIn()) {
        response.sendRedirect(request.getContextPath() + "/login.xhtml"); // No logged-in user found, so redirect to login page.
    } else {
        chain.doFilter(req, res); // Logged-in user found, so just continue request.
    }
}

Where UserManager represents your session scoped JSF managed bean.

在这里,UserManager表示您的会话作用域的JSF托管bean。

See also:

#2


0  

Also check @ManagedProperty is correct. For example:

也检查@ManagedProperty是否正确。例如:

@ManagedProperty(value = "#{user_ejb}")
UserEJB userEJB;

Or @Inject

或@ inject