严重:处理请求期间发生异常:null java.lang.NullPointerException

时间:2015-09-17 11:34:51

标签: java xml validation struts2 nullpointerexception

我正在尝试在eclipse中的Struts2中创建一个基本的登录/注册。我得到的例外情况是我无法解决的,我正在尝试最近几个小时:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
   "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <include file="struts-default.xml"/>
    <package name="a" extends="struts-default">
        <action name="loginauth" class="com.project.loginreg.LoginAction" method="execute" >
           <result name="SUCCESS" type="redirect">
        <param name="location">/welcome.jsp</param>
        </result>
            <result name="ERROR">/login.jsp</result>
        </action>
    </package>
</struts>

Web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Login_Reg</display-name>

<filter>
    <filter-name>struts2</filter-name>
<filter-class>
       org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter                
</filter-class>
</filter>

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

以下是我正在制作的问题详情

Sep 17, 2015 4:54:41 PM org.apache.struts2.dispatcher.DefaultDispatcherErrorHandler error
SEVERE: Exception occurred during processing request: null
java.lang.NullPointerException
    at com.project.loginreg.LoginAction.execute(LoginAction.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:870)
    at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1293)
    at ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)
    at com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethodWithDebugInfo(XWorkMethodAccessor.java:117)
    at com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethod(XWorkMethodAccessor.java:108)
    at ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1369)
    at ognl.ASTMethod.getValueBody(ASTMethod.java:90)
    at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
    at ognl.SimpleNode.getValue(SimpleNode.java:258)
    at ognl.Ognl.getValue(Ognl.java:494)
    at ognl.Ognl.getValue(Ognl.java:458)
    at com.opensymphony.xwork2.ognl.OgnlUtil$2.execute(OgnlUtil.java:309)
    at com.opensymphony.xwork2.ognl.OgnlUtil.compileAndExecute(OgnlUtil.java:340)
    at com.opensymphony.xwork2.ognl.OgnlUtil.getValue(OgnlUtil.java:307)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:423)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:287)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:250)
    at org.apache.struts2.interceptor.DeprecationInterceptor.intercept(DeprecationInterceptor.java:41)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:167)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:76)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:229)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:229)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at org.apache.struts2.interceptor.DateTextFieldInterceptor.intercept(DateTextFieldInterceptor.java:125)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:253)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:139)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:564)
    at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

继承LoginAction:

package com.project.loginreg;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{

    private static final long serialVersionUID = 1L;
    private String userId;
    private String password;

    private Boolean error;

    public String execute(){
        if (userId.equals("")) {
            addActionError("Please Enter user id.");
            error = true;
        }
        else if(password.equals("")){
            addActionError("Please Enter Password.");
            error = true;
        }
         else if(!userId.equalsIgnoreCase(password)) {
             addActionError("Invalid userid or password.");
             error = true;
         } else {
             error = false;
         }
        if (error) {
            return ERROR;
        } else {
            return SUCCESS;
        }   
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

3 个答案:

答案 0 :(得分:2)

你的

private String userId

未初始化。它是NULL。 如果你想匹配一个空字符串,你必须先将它初始化。

所以将其改为

private String userId = "";

答案 1 :(得分:2)

你在深水中...:|顺便说一下,简而言之:

  

防止NPE

要在比较字符串时阻止NPE,请将其与已知字符串进行比较:

if (userId.equals("")) { // throws NPE
if ("".equals(userId)) { // it's ok

在你的代码中你应该检查null,所以两者都可以:

if (userId==null || userId.equals("")) {
  

正确的地方:验证

现在它们正常运行,删除所有这些检查并将它们in the validate() method, or in a validation.xml file放入,或使用注释进行验证。 Struts支持所有这些方法,returns the INPUT result when things go south

永远不要在行动方法中使用这种检查,这是错误的地方。

  

正确处理身份验证的Struts2方法:拦截器

不要使用操作来执行此操作,而是创建一个身份验证拦截器,将其应用于除登录/注销之外的所有操作。

full working example here

答案 2 :(得分:0)

您的私有变量<svg class="main-logo" width="980" height="598" viewBox="0 0 1500 650" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <title>Logo</title> <desc>Main Logo</desc> <g id="Canvas" transform="translate(-20990 -1876)"> <clipPath id="clip-0" clip-rule="evenodd"> <path d="M 20990 1876L 21970 1876L 21970 2474L 20990 2474L 20990 1876Z" fill="#FFFFFF"/> </clipPath> <g id="Frame" clip-path="url(#clip-0)"> <g id="Group 2.3"> <g id="Group"> <g id="Subtract"> <a class = "about-me-logo-link" href = "about-me.html"><use xlink:href="#yellow" transform="translate(21507.5 1874)" fill="#F2C94C"/></a> </g> <g id="Vector"> <a class = "resume-logo-link" href="resume.html"><use xlink:href="#green" transform="translate(21439.6 1934.83)" fill="#27AE60"/></a> </g> <g id="Vector"> <a class = "media-logo-link" href="media.html"><use xlink:href="#grey" transform="translate(21196.4 1934.82)" fill="#4F4F4F"/></a> </g> </g> <g id="Subtract"> <a class = "blog-logo-link" href="blog.html"><use xlink:href="#blue" transform="translate(20990 1874)" fill="#2F80ED"/></a> </g> </g> <g id="FEHRNSTROM"> <use xlink:href="#path4_fill" transform="matrix(0.459551 -0.888151 0.888151 0.459551 21601 2423.85)"/> </g> </g> </g> <defs> <path id="yellow" fill-rule="evenodd" d="M 221.328 0L 0 415.462L 93.4941 550.935L 381.551 0L 221.328 0Z"/><!--Yellow--> <path id="green" fill-rule="evenodd" d="M 161.675 490.11L 0 256.658L 0.0265208 0L 245.883 328.391L 161.675 490.11Z"/><!--Green--> <path id="grey" fill-rule="evenodd" d="M 243.115 0L 0 333.587L 81.3885 490.182L 335.524 123.363L 243.171 0.0759988L 243.115 0Z"/><!--Gray--> <path id="blue" fill-rule="evenodd" d="M 0 0L 288.092 551L 385.535 409.802L 162.213 0L 0 0Z"/> <!--Blue--> <path id="text" d="M 47.1377 60.3789L 21.1592 60.3789L 21.1592 87L 5.04883 87L 5.04883 20.1719L 49.7539 20.1719L 49.7539 32.6104L 21.1592 32.6104L 21.1592 47.9863L 47.1377 47.9863L 47.1377 60.3789ZM 97.9014 58.6807L 72.5654 58.6807L 72.5654 74.6074L 102.491 74.6074L 102.491 87L 56.4551 87L 56.4551 20.1719L 102.583 20.1719L 102.583 32.6104L 72.5654 32.6104L 72.5654 46.7012L 97.9014 46.7012L 97.9014 58.6807ZM 164.913 87L 148.895 87L 148.895 59.0938L 125.165 59.0938L 125.165 87L 109.055 87L 109.055 20.1719L 125.165 20.1719L 125.165 46.7012L 148.895 46.7012L 148.895 20.1719L 164.913 20.1719L 164.913 87ZM 199.979 63.4082L 191.259 63.4082L 191.259 87L 175.148 87L 175.148 20.1719L 201.448 20.1719C 209.373 20.1719 215.57 21.9313 220.037 25.4502C 224.505 28.9691 226.738 33.9414 226.738 40.3672C 226.738 45.0182 225.79 48.8737 223.893 51.9336C 222.026 54.9935 219.089 57.472 215.08 59.3691L 229.033 86.3115L 229.033 87L 211.775 87L 199.979 63.4082ZM 191.259 50.9697L 201.448 50.9697C 204.508 50.9697 206.803 50.1742 208.333 48.583C 209.894 46.9613 210.674 44.7122 210.674 41.8359C 210.674 38.9596 209.894 36.7106 208.333 35.0889C 206.772 33.4365 204.478 32.6104 201.448 32.6104L 191.259 32.6104L 191.259 50.9697ZM 292.281 87L 276.263 87L 252.579 45.416L 252.579 87L 236.469 87L 236.469 20.1719L 252.579 20.1719L 276.217 61.7559L 276.217 20.1719L 292.281 20.1719L 292.281 87ZM 336.94 69.2373C 336.94 66.8812 336.099 65.0452 334.416 63.7295C 332.764 62.4137 329.841 61.0521 325.649 59.6445C 321.457 58.237 318.03 56.8753 315.368 55.5596C 306.709 51.3063 302.379 45.4619 302.379 38.0264C 302.379 34.3239 303.45 31.0651 305.592 28.25C 307.764 25.4043 310.824 23.2012 314.771 21.6406C 318.719 20.0495 323.156 19.2539 328.082 19.2539C 332.886 19.2539 337.185 20.1107 340.979 21.8242C 344.804 23.5378 347.772 25.9857 349.884 29.168C 351.995 32.3197 353.051 35.9303 353.051 40L 336.986 40C 336.986 37.2767 336.145 35.1654 334.462 33.666C 332.81 32.1667 330.561 31.417 327.715 31.417C 324.839 31.417 322.559 32.0596 320.876 33.3447C 319.224 34.5993 318.397 36.2057 318.397 38.1641C 318.397 39.8776 319.315 41.4382 321.151 42.8457C 322.987 44.2227 326.215 45.6608 330.836 47.1602C 335.456 48.6289 339.251 50.2201 342.219 51.9336C 349.44 56.0951 353.051 61.8324 353.051 69.1455C 353.051 74.9899 350.848 79.5798 346.441 82.915C 342.035 86.2503 335.992 87.918 328.312 87.918C 322.896 87.918 317.984 86.9541 313.578 85.0264C 309.202 83.068 305.898 80.4059 303.664 77.04C 301.461 73.6436 300.359 69.7422 300.359 65.3359L 316.516 65.3359C 316.516 68.916 317.434 71.5628 319.27 73.2764C 321.136 74.9593 324.15 75.8008 328.312 75.8008C 330.974 75.8008 333.07 75.2347 334.6 74.1025C 336.16 72.9398 336.94 71.318 336.94 69.2373ZM 413.774 32.6104L 393.717 32.6104L 393.717 87L 377.606 87L 377.606 32.6104L 357.916 32.6104L 357.916 20.1719L 413.774 20.1719L 413.774 32.6104ZM 445.444 63.4082L 436.724 63.4082L 436.724 87L 420.613 87L 420.613 20.1719L 446.913 20.1719C 454.838 20.1719 461.035 21.9313 465.502 25.4502C 469.969 28.9691 472.203 33.9414 472.203 40.3672C 472.203 45.0182 471.255 48.8737 469.357 51.9336C 467.491 54.9935 464.553 57.472 460.545 59.3691L 474.498 86.3115L 474.498 87L 457.24 87L 445.444 63.4082ZM 436.724 50.9697L 446.913 50.9697C 449.973 50.9697 452.268 50.1742 453.798 48.583C 455.358 46.9613 456.139 44.7122 456.139 41.8359C 456.139 38.9596 455.358 36.7106 453.798 35.0889C 452.237 33.4365 449.942 32.6104 446.913 32.6104L 436.724 32.6104L 436.724 50.9697ZM 538.526 54.9629C 538.526 61.4805 537.318 67.2484 534.9 72.2666C 532.483 77.2848 529.041 81.1556 524.573 83.8789C 520.136 86.5716 515.072 87.918 509.381 87.918C 503.689 87.918 498.641 86.6175 494.234 84.0166C 489.828 81.3851 486.386 77.6367 483.907 72.7715C 481.459 67.9062 480.174 62.3219 480.052 56.0186L 480.052 52.2549C 480.052 45.7067 481.245 39.9388 483.632 34.9512C 486.049 29.9329 489.492 26.0622 493.959 23.3389C 498.457 20.6156 503.567 19.2539 509.289 19.2539C 514.95 19.2539 519.999 20.6003 524.436 23.293C 528.872 25.9857 532.315 29.8258 534.763 34.8135C 537.241 39.7705 538.496 45.4619 538.526 51.8877L 538.526 54.9629ZM 522.141 52.1631C 522.141 45.5231 521.024 40.4896 518.79 37.0625C 516.587 33.6048 513.42 31.876 509.289 31.876C 501.211 31.876 496.942 37.9346 496.483 50.0518L 496.438 54.9629C 496.438 61.5111 497.524 66.5446 499.696 70.0635C 501.869 73.5824 505.097 75.3418 509.381 75.3418C 513.451 75.3418 516.587 73.613 518.79 70.1553C 520.993 66.6976 522.11 61.7253 522.141 55.2383L 522.141 52.1631ZM 567.993 20.1719L 582.91 66.667L 597.781 20.1719L 618.986 20.1719L 618.986 87L 602.83 87L 602.83 71.3945L 604.391 39.4492L 588.234 87L 577.586 87L 561.384 39.4033L 562.944 71.3945L 562.944 87L 546.834 87L 546.834 20.1719L 567.993 20.1719Z"/> </defs> </svg>未初始化。这个评论是正确的。 但是,您应该编写一个set函数

userId

初始化您的变量而不是

public void setUserId(String userId){
    this.userId =userId;
}

我和你的问题一样,这是我的解决方案。