How can I remove NullPointerException in Struts1.x
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..
<< 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
<< 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 - Extract
<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
< %@ 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
package com.example;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.acti开发者_JAVA百科on.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
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,
AnkitYou forgot a /
in your action on html:form
.
This is how you should call your action in the form.
<html:form action="/login">
Update, Make sure you clean all your XML documents including your JSP's
The following (examples) were not allowed in the XML validator:
< form-beans>
< /struts-config>
XML doesn't allow spaces inside the <></>
, </>
tags. This also applies to the tags too.
Update, I found the correct problem. It's your declaration for form beans in your 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.
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.
Check Your jsp.Its using login as action form instead of LoginForm.
Well that error is because of you have not configure you Struts-config.xml properly.
you have to give particular Form-Bean name when action of the Form is called Accordingly.
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.
for example:
< action path="/loginPre" name="loginRequest" forward="/youloginformname.jsp">
</action>
that way problem will get solved.
精彩评论