我在UserBean中定義了一個方法:
public String verify(){
......
}index.jsp中用了這樣的表達式:
<h:commandButton value="送出"
action="#{user.verify}"/>可是運行時報空指針異常:
java.lang.NullPointerException
at vo.UserBean.verify(UserBean.java:31)31行就是定義方法的位置:public String verify()請各位指點!

解决方案 »

  1.   

    user 是在jsf的配置文件中配置的类的别名,楼主是否有写呢 ?
    写个类似的给楼主<managed-bean>
    <managed-bean-name>user </managed-bean-name>
    <managed-bean-class>
    com.web.user 
    </managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
      

  2.   

    package vo;
    public class UserBean {
    private String name;
    private String password;

    public String getPassword() {
    return password;
    } public void setPassword(String password) {
    this.password = password;
    } public String getErrMessage() {
    return errMessage;
    } public void setErrMessage(String errMessage) {
    this.errMessage = errMessage;
    } private String errMessage; public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    }
    public String verify(){
    if(!name.equals("chen")||!password.equals("123")){
    errMessage="密碼或名稱錯誤!";
    return "failure";
    }
    else{
    return "success";
    } }
    }<?xml version="1.0" encoding="ISO-8859-1"?>
    <faces-config>
    <navigation-rule>
    <from-view-id>index.jsp</from-view-id>
    <navigation-case>
    <from-outcome>success</from-outcome>
    <to-view-id>welcome.jsp</to-view-id>
    </navigation-case>
    <navigation-case>
    <from-outcome>failure</from-outcome>
                <to-view-id>index.jsp</to-view-id>
    </navigation-case> </navigation-rule>
    <managed-bean>
    <managed-bean-name>user</managed-bean-name>
    <managed-bean-class>
    vo.UserBean
    </managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
    </faces-config><%@ page language="java" pageEncoding="GB2312"%>
    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>First JSF Project</title>
    </head> <body>
    <f:view>
    <h:form>
    <h3>
    Please cint your name!
    </h3>
    <h:outputText value="#{user.errMessage}" /><p>
             NAME:<h:inputText value="#{user.name}" /><p>
            PWD:<h:inputSecret value="#{user.password}" /><p>
    <h:commandButton value="Go" action="#{user.verify}" />
    </h:form>
    </f:view>
    </body>
    </html><%@ page language="java" pageEncoding="GB2312"%>
    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %><%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <base href="<%=basePath%>"> <title>My JSF 'welcome.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    --></head>
      
    <body>
    <f:view>
    <h:outputText value="#{user.name}"/> 您好!
    <h3>欢迎使用 JavaServer Faces!</h3>
    </f:view>
    </body>
    </html>
      

  3.   

    package vo;
    public class UserBean {
    private String name;
    private String password;public String getPassword() {
    return password;
    }public void setPassword(String password) {
    this.password = password;
    }public String getErrMessage() {
    return errMessage;
    }public void setErrMessage(String errMessage) {
    this.errMessage = errMessage;
    }private String errMessage;public String getName() {
    return name;
    }public void setName(String name) {
    this.name = name;
    }
    public String verify(){
    if(!name.equals("chen")||!password.equals("123")){
    errMessage="密碼或名稱錯誤!";
    return "failure";
    }
    else{
    return "success";
    }}
    }
    这是faces-config.xml:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <faces-config>
    <navigation-rule>
    <from-view-id>index.jsp</from-view-id>
    <navigation-case>
    <from-outcome>success</from-outcome>
    <to-view-id>welcome.jsp</to-view-id>
    </navigation-case>
    <navigation-case>
    <from-outcome>failure</from-outcome>
      <to-view-id>index.jsp</to-view-id>
    </navigation-case></navigation-rule>
    <managed-bean>
    <managed-bean-name>user</managed-bean-name>
    <managed-bean-class>
    vo.UserBean
    </managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
    </faces-config>
    这是index.jsp:
    <%@ page language="java" pageEncoding="GB2312"%>
    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>First JSF Project</title>
    </head><body>
    <f:view>
    <h:form>
    <h3>
    Please cint your name!
    </h3>
    <h:outputText value="#{user.errMessage}" /><p>
    NAME:<h:inputText value="#{user.name}" /><p>
    PWD:<h:inputSecret value="#{user.password}" /><p>
    <h:commandButton value="Go" action="#{user.verify}" />
    </h:form>
    </f:view>
    </body>
    </html>
    这是welcome.jsp:
    <%@ page language="java" pageEncoding="GB2312"%>
    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %><%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <base href="<%=basePath%>"><title>My JSF 'welcome.jsp' starting page</title><meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">  
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    --></head>
      
    <body>
    <f:view>
    <h:outputText value="#{user.name}"/> 您好!
    <h3>欢迎使用 JavaServer Faces!</h3>
    </f:view>
    </body>
    </html>
      

  4.   

    2011/02/14 11:14:13 com.sun.faces.application.ActionListenerImpl processAction
    致命的: java.lang.NullPointerException
    javax.faces.el.EvaluationException: java.lang.NullPointerException
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    at javax.faces.component.UICommand.broadcast(UICommand.java:387)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:475)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:756)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:265)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:619)
    Caused by: java.lang.NullPointerException
    at vo.UserBean.verify(UserBean.java:31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:191)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
    at org.apache.jasper.el.JspMethodExpression.invoke(JspMethodExpression.java:68)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    ... 20 more
    2011/02/14 11:14:13 com.sun.faces.lifecycle.InvokeApplicationPhase execute
    警告: #{user.verify}: java.lang.NullPointerException
    javax.faces.FacesException: #{user.verify}: java.lang.NullPointerException
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
    at javax.faces.component.UICommand.broadcast(UICommand.java:387)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:475)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:756)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:265)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:619)
    Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    ... 19 more
    Caused by: java.lang.NullPointerException
    at vo.UserBean.verify(UserBean.java:31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:191)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
    at org.apache.jasper.el.JspMethodExpression.invoke(JspMethodExpression.java:68)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    ... 20 more
    2011/02/14 11:14:13 com.sun.faces.lifecycle.Phase doPhase
    致命的: JSF1054: (Phase ID: INVOKE_APPLICATION 5, View ID: /index.jsp) Exception thrown during phase execution: javax.faces.event.PhaseEvent[source=com.sun.faces.lifecycle.LifecycleImpl@ae97c4]
    2011/02/14 11:14:13 org.apache.catalina.core.StandardWrapperValve invoke
    致命的: サーブレット Faces Servlet のServlet.service()が例外を投げました
    java.lang.NullPointerException
    at vo.UserBean.verify(UserBean.java:31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:191)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
    at org.apache.jasper.el.JspMethodExpression.invoke(JspMethodExpression.java:68)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    at javax.faces.component.UICommand.broadcast(UICommand.java:387)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:475)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:756)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:265)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:619)
      

  5.   

    if(!name.equals("chen")||!password.equals("123"))
    -->
    if(!"chen".equals(name)||!"123".equals(password))