File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Getting null pointer Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Getting null pointer Exception" Watch "Getting null pointer Exception" New topic
Author

Getting null pointer Exception

Sam mit
Greenhorn

Joined: Sep 11, 2010
Posts: 3
Hi,
I wrote one simple struts program for login page.

struts-config.xml:--------------------------
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE struts-config SYSTEM "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
<form-beans >
<form-bean name="loginBean" type="com.sam.bean.loginBean" />
</form-beans>

<action-mappings>
<action path="/login" name = "loginBean" type = "com.sam.action.loginAction"
scope= "request" input="/login.jsp">
<forward name="success" path="/home.jsp"></forward>
<forward name="failure" path="/login.jsp"></forward>
</action>
</action-mappings>

<!-- <message-resources parameter="MessageResources" /> -->
</struts-config>


login.jsp:--------------------

<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
</head>
<body>
<html:form action="/login.do">
<table>
<tr>
<td>User Id :</td>
<td><html:text property="text" name="userId"/></td>
</tr>

<tr>
<td>Password :</td>
<td><html:text property="password" name="password"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<html:submit value= "login" ></html:submit>
</td>
</tr>
</table>
</html:form>


</body>
</html>

-----------------------------------------

home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome Struts Project </title>
</head>
<body>
<h1>Welcome Struts Project </h1>
<h1>
Hi <bean:write name ="userId" scope = "request"/>
</h1>

</body>
</html>

------------------------------

loginAction.java

package com.sam.action;

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

import com.sam.bean.loginBean;

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

public class loginAction extends Action{

public ActionForward execute(ActionMapping am , ActionForm af,
HttpServletRequest req, HttpServletResponse res) throws Exception
{
loginBean lb = (loginBean)af;
String userId = lb.getUserId();
String password = lb.getPassword();

String str = "failure";
if(userId.equals(password))
{
str = "success";
req.setAttribute("userId", userId);
}

ActionForward aFwd = am.findForward(str);
return aFwd;
}

}

-----------------------------------

loginBean.java

package com.sam.bean;

import org.apache.struts.action.ActionForm;

public class loginBean extends ActionForm{

private String userId;
private String password;

public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}

}


-----------------------------------------

web.xml:

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE web-app SYSTEM "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<display-name>Struts Examples Application</display-name>
<!-- Standard Action Servlet Configuration (with debugging) -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<!-- module configurations -->

<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>

<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- The Usual Welcome File List -->

<!-- Struts Tag Library Descriptors -->
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-nested</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>
<!-- tiles not used here
<taglib>
<taglib-uri>/tags/struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>
-->
</web-app>


---------------------------------

Error getting:

url:http://localhost:8080/StrutsProject/login.do

HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:516)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:423)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:397)
javax.servlet.http.HttpServlet.service(HttpServlet.java:627)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)


root cause

java.lang.NullPointerException
com.sam.action.loginAction.execute(loginAction.java:23)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:397)
javax.servlet.http.HttpServlet.service(HttpServlet.java:627)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.30 logs.


--------------------------------------------------------------------------------

Apache Tomcat/5.5.30



can anybody solve my problem?
Please tell me how to rum this program??

Thanks in advance
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Welcome to JavaRanch! Please UseCodeTags when posting code or configuration; unformatted code is surprisingly difficult to read, and you'll find you're more likely to get help when your code is as easy to read as possible. Thanks!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Plus we have no real way of knowing which line of your code the exception actually occurs on--it's useful to indicate that somehow.
Sam mit
Greenhorn

Joined: Sep 11, 2010
Posts: 3
login.jsp

web.xml


home.jsp


loginBean.java



loginAction.java


in LoginAction.java class.. i put the comment on the error line..
Please suggest me...
Pravin Shirke
Ranch Hand

Joined: Apr 05, 2008
Posts: 150

Hi sam mit,
you are getting null pointer exception then it means you are not getting any value in userId. kindly check whether userId is getting populated or not. I think its not getting populated. and you are trying to call method on null so its giving you the exception. check at line 20 and 21 print it and see if you get value for user id and password in loginAction.

hope this helps


[Vipassana] - It is seeing the reality as it is, And not as you want it to be.!!!
SCJP1.5.
ayyappan Bas
Ranch Hand

Joined: Oct 11, 2008
Posts: 39
hi

Very simple
I changed like this .it should work .

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


Thanks & Regards
Ayyappan
Sam mit
Greenhorn

Joined: Sep 11, 2010
Posts: 3
Thanks ayyappan Bas,
Its working now.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Getting null pointer Exception
 
Similar Threads
Servlet Action not found
Validation in struts
Struts with Tiles--- simple program but not working
what does .do file extension indicate
Problem facing in Form bean