• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting null pointer Exception

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 152
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

Very simple
I changed like this .it should work .

<html:text property="userId" />
<html:text property="password" />
 
Sam mit
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ayyappan Bas,
Its working now.
 
reply
    Bookmark Topic Watch Topic
  • New Topic