java 如何删除 Struts1.x 中的 NullPointerException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6451516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:55:59  来源:igfitidea点击:

How can I remove NullPointerException in Struts1.x

javastruts-1

提问by user751786

I am new to STRUTS and am trying to work out simple example . When I execute the example I get the following error on the internet browser..

我是 STRUTS 的新手,正在尝试找出简单的例子。当我执行该示例时,我在 Internet 浏览器上收到以下错误..

<< javax.servlet.ServletException: javax.servlet.jsp.JspException: Exception creating bean of class com.example.LoginForm >>

and in the Eclipse console see a similar error with additional line on Null pointer exception

并在 Eclipse 控制台中看到类似的错误,并在 Null 指针异常上添加了一行

<< SEVERE: Error creating form bean of class com.example.LoginForm
java.lang.NullPointerException >>

Listed below is part of my struts-config.xml

下面列出的是我的一部分 struts-config.xml

struts-config.xml - Extract

struts-config.xml - 提取

<struts-config>

< form-beans>   
  < form-bean name="loginRequest" type="com.example.LoginForm" />    
</form-bean>
< /form-beans>


< !-- =========================================== Global Forward Definitions -->

< global-forwards>  
< forward
   name="welcome"
   path="/Welcome.do"/>  
< /global-forwards>


< !-- =========================================== Action Mapping Definitions -->

< action-mappings>   
< action path="/login"
    name="loginRequest"
    type="com.example.LoginAction">   
 < forward name="success"
     path="/success.jsp">
 < /forward>    
 < forward name="failure"
     path="/login.jsp">
  < /forward>
 </action>    
 <action path="/Welcome"
    forward="/pages/Welcome.jsp"/>
< /action-mappings>


< !-- ======================================== Message Resources Definitions -->

< message-resources parameter="MessageResources" />

< /struts-config>    

My JSP login.jsp

我的 JSP 登录.jsp

< %@ taglib prefix="bean" uri="/WEB-INF/struts-bean.tld" %>    
< %@ taglib prefix="html" uri="/WEB-INF/struts-html.tld" %>

< bean:message key="title" />

< html:form action="login">

    <bean:message key="login.username"/>

    <html:text property="username"></html:text>

    <br>

    <bean:message key="login.password"/>

    <html:text property="password"></html:text>

    <br>

    <html:submit>
        <bean:message key="login.submit"/>
    </html:submit>

< /html:form>

The LoginForm class

登录表单类

package com.example;

import org.apache.struts.action.ActionForm;

public class LoginForm extends ActionForm     
{     
    private String username;   
    private String password;

    public LoginForm()
    {

    }

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

}

The LoginAction class

LoginAction 类

package com.example;   

import javax.servlet.http.HttpServletRequest;    
import javax.servlet.http.HttpServletResponse;     

import org.apache.struts.action.Action;     
import org.apache.struts.action.ActionForm;     
import org.apache.struts.action.ActionForward;      
import org.apache.struts.action.ActionMapping;     

public class LoginAction extends Action   
{     
    @Override    
    public ActionForward execute(ActionMapping mapping, ActionForm form,    
            HttpServletRequest request, HttpServletResponse response)    
            throws Exception     
    {   
        LoginForm login = (LoginForm)form;     
        String name = login.getUsername();    
        String pass = login.getPassword();    

        if(name.equals("hello") && pass.equals("hello"))    
            return mapping.findForward("success");   
        else    
            return mapping.findForward("failure");    
    }    
}   

The MessageResources.properties class

MessageResources.properties 类

title=Welcome   
login.username=Username    
loguin.password=Password    
login.submit=Submit

Stack Trace

堆栈跟踪

org.apache.struts.util.RequestUtils createActionForm    
SEVERE: Error creating form bean of class com.example.LoginForm 
java.lang.NullPointerException
=========== ======================
    at org.apache.struts.config.FormBeanConfig.createActionForm(FormBeanConfig.java:289)
    at org.apache.struts.util.RequestUtils.createActionForm(RequestUtils.java:259)
    at org.apache.struts.util.RequestUtils.createActionForm(RequestUtils.java:213)
    at org.apache.struts.taglib.html.FormTag.initFormBean(FormTag.java:526)
    at org.apache.struts.taglib.html.FormTag.doStartTag(FormTag.java:503)
    at org.apache.jsp.login_jsp._jspx_meth_html_005fform_005f0(login_jsp.java:122)
    at org.apache.jsp.login_jsp._jspService(login_jsp.java:79)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    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:128)
    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:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    at java.lang.Thread.run(Unknown Source)

Jun 23, 2011 12:56:47 PM org.apache.struts.util.PropertyMessageResources loadLocale
WARNING:   Resource org/apache/struts/taglib/html/LocalStrings_en_US.properties Not Found.
Jun 23, 2011 12:56:47 PM org.apache.struts.util.PropertyMessageResources loadLocale
WARNING:   Resource org/apache/struts/taglib/html/LocalStrings_en.properties Not Found.
Jun 23, 2011 12:56:47 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet jsp threw exception <br/><br/>
javax.servlet.jsp.JspException: Exception creating bean of class com.example.LoginForm under form name loginRequest
    at org.apache.struts.taglib.html.FormTag.initFormBean(FormTag.java:536)
    at org.apache.struts.taglib.html.FormTag.doStartTag(FormTag.java:503)
    at org.apache.jsp.login_jsp._jspx_meth_html_005fform_005f0(login_jsp.java:122)
    at org.apache.jsp.login_jsp._jspService(login_jsp.java:79)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    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:128)
    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:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    at java.lang.Thread.run(Unknown Source)

Can anyone figure out what I am doing wrong and why this error is occuring.

谁能弄清楚我做错了什么以及为什么会发生这个错误。

Thanks,
Ankit

谢谢,
安吉特

回答by Buhake Sindi

You forgot a /in your action on html:form.

/在你的行动中忘记了html:form

This is how you should call your action in the form.

这就是您应该如何在表单中调用您的操作。

<html:form action="/login">


Update, Make sure you clean allyour XML documents including your JSP's

更新,确保清理所有XML 文档,包括 JSP

The following (examples) were not allowed in the XML validator:

XML 验证器中不允许使用以下(示例):

  • < form-beans>
  • < /struts-config>
  • < form-beans>
  • < /struts-config>

XML doesn't allow spaces inside the <></>, </>tags. This also applies to the tags too.

XML 不允许在<></>,</>标签内有空格。这也适用于标签。



Update, I found the correctproblem. It's your declaration for form beans in your struts-config.xml.

更新,我找到了正确的问题。这是您在struts-config.xml.

You have:

你有:

<form-beans>   
    <form-bean name="loginRequest" type="com.example.LoginForm" />    
</form-bean>
</form-beans>

You're closing your <form-bean>twice.

你正在关闭你的<form-bean>两次

Solution:

解决方案:

< form-beans>
    <form-bean name="loginRequest" type="com.example.LoginForm" />    
</form-beans>

Now, this must work. :-) (Sorry for the late reply, work demands me.)

现在,这必须奏效。:-)(抱歉回复晚了,工作需要我。

Also, I suggest using an IDE such as Eclipse or NetBeans to do your Struts project (and not do this by hand-typing) especially for your XML declarations.

另外,我建议使用 Eclipse 或 NetBeans 之类的 IDE 来执行 Struts 项目(而不是通过手动输入来执行此操作),尤其是对于 XML 声明。

回答by human.js

Check Your jsp.Its using loginas action form instead of LoginForm.

检查您的 jsp.Its 使用登录作为操作表单而不是LoginForm

回答by Krishna

Well that error is because of you have not configure you Struts-config.xml properly.

那么这个错误是因为你没有正确配置你的 Struts-config.xml。

you have to give particular Form-Bean name when action of the Form is called Accordingly.

当相应地调用表单的操作时,您必须给出特定的 Form-Bean 名称。

now look at you code you have give action like this.

现在看看你的代码,你有这样的动作。

< action path="/login" name="loginRequest" type="com.example.LoginAction">   
 < forward name="success" path="/success.jsp">< /forward>

but while u are calling POJO also together but you have to call that bean before you make POST action for putting any data into the user interaction. so make one new action which can call bean class before you main action is called.

但是,虽然您也一起调用 POJO,但是您必须在执行 POST 操作以将任何数据放入用户交互之前调用该 bean。因此,在调用主要操作之前,创建一个可以调用 bean 类的新操作。

for example:

例如:

< action path="/loginPre" name="loginRequest" forward="/youloginformname.jsp">  
</action>

that way problem will get solved.

这样问题就会得到解决。