wood burning stoves 2.0*
The moose likes Servlets and the fly likes NullPointer Exception When calling  RequestDispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "NullPointer Exception When calling  RequestDispatcher" Watch "NullPointer Exception When calling  RequestDispatcher" New topic
Author

NullPointer Exception When calling RequestDispatcher

ganesh pol
Ranch Hand

Joined: Apr 29, 2005
Posts: 151
friends i have got following Error in my code

HTTP Status 500 -

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

type Exception report

message

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

exception

java.lang.NullPointerException
at com.leaveapp.servlets.LoginServlets.doPost(LoginServlets.java:95)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:260)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2396)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:405)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:380)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:508)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:533)
at java.lang.Thread.run(Thread.java:534)



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

Apache Tomcat/4.1.12


my Servlet code is

package com.leaveapp.servlets ;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.util.*;
import com.leaveapp.utils.*;
import javax.servlet.*;


/* this is Servlet used to check whether user is valid or not for that it uses password as Key */

public class LoginServlets extends HttpServlet {

private Map users= new Hashtable();
private String form_id;
private String username;
private String password;

public void init() {

users.put("ganesh","g.pol");
users.put("poonam","p.gupta");
users.put("vivek","v.padh");
users.put("jayesh","j.khanvilkar");
}

/*setter methods for Servlet */
private void setForm_id(String fi){
this.form_id=fi;
}
private void setUsername(String un){

this.username=un;
}

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

/* getter methods for Servlet */
private String getForm_id()
{
return form_id;
}
private String getUsername()
{
return form_id;
}
private String getPassword()
{
return form_id;
}




public boolean checkuser(String form_id, String username ,String password ) {

if (form_id !=null && form_id.equals("loginform"))
{
if(username != null && password != null && username.equals((String) users.get(password)))
{
return true;
}
else {
return false;
}

}
return false;
}

public void doPost(HttpServletRequest request,
HttpServletResponse response) throws IOException,ServletException {

/*getting parameters from request */
form_id = request.getParameter(Constants.FORM_ID);
username=request.getParameter(Constants.USER_NAME);
password=request.getParameter(Constants.USER_PASSWORD);

setForm_id(form_id);
setUsername(username);
setPassword(password);
System.out.println("saaasas min post");
if(checkuser(getForm_id(),getUsername(),getPassword()))
{ /* he is valid user hence do necessary action*/
System.out.println("you are valid user");
}
else <------------- main problem Area
{ System.out.println("you are elsr");
RequestDispatcher rd=request.getRequestDispatcher("../Jsp/login/Loginerror.jsp");
System.out.println("you are RequestDispatcher"+rd); <----Request Dipatcher is null
rd.forward(request,response);
// request.getContext.getRequestDispatcher("../Jsp/login/Loginerror.jsp").forward(request,response);

}
}//end of post

}


on Server side i have got

08-Aug-2005 18:24:12 org.apache.commons.modeler.Registry loadRegistry
INFO: Loading registry information
08-Aug-2005 18:24:12 org.apache.commons.modeler.Registry getRegistry
INFO: Creating new Registry instance
08-Aug-2005 18:24:13 org.apache.commons.modeler.Registry getServer
INFO: Creating MBeanServer
08-Aug-2005 18:24:13 org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on port 8080
Starting service Tomcat-Standalone
Apache Tomcat/4.1.12
08-Aug-2005 18:24:16 org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on port 8080
08-Aug-2005 18:24:16 org.apache.jk.common.ChannelSocket init
INFO: JK2: ajp13 listening on tcp port 8009
08-Aug-2005 18:24:16 org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=16/31 config=C:\Program Files\Apache Group\Tomcat 4.1\conf\jk2.properties
saaasas min post
you are elsr
you are RequestDispatchernull <------------here Request Dispatcher objet is null






Look For your Kind HElp
Thanks in Advance
Kerry Wilson
Ranch Hand

Joined: Oct 29, 2003
Posts: 254
Where is the JSP directory located?

You should probably call without .. just lead with slash.


http://www.goodercode.com
SCJP 1.4
ganesh pol
Ranch Hand

Joined: Apr 29, 2005
Posts: 151
i have solved that problem bi have change jsp path as
RequestDispatcher rd=request.getRequestDispatcher("jsp/login/Loginerror.jsp");

and it shows LoginError page

but although when i have enter valid username and Password it gives error page

when i am calling
if(checkuser(getForm_id(),getUsername(),getPassword()))

control goes to

public boolean checkuser(String form_id, String username ,String password ) {
System.out.println(getForm_id()+ " in Checkuser");
if (form_id !=null && form_id.equals("loginform"))
{ System.out.println(getForm_id()+ "in Checkuser if 1");
if(username != null
&& password != null
&& username.equals((String)users.get(password)))
<-----this condition fails
{ System.out.println(getForm_id()+ "in Checkuser if 2");
return true;
}
else { System.out.println(getForm_id()+ "in Checkuser else ");
return false;
}

}
return false;
}


public void init() { <<----- here username and password defined

users.put("ganesh","g.pol");
users.put("poonam","p.gupta");
users.put("vivek","v.padh");
users.put("jayesh","j.khanvilkar");

}


my question is how to get username and password but not using Servletconfig and ServletContext
stan ioffe
Greenhorn

Joined: Jun 30, 2005
Posts: 16
Where is the "form_id" property set to "loginform"??

In the init(), you should set the form_id parameter to "loginform" since you're checking it later.

Then it should work ...

-stan
 
jQuery in Action, 2nd edition
 
subject: NullPointer Exception When calling RequestDispatcher
 
Similar Threads
mySQL insert pukes on me
problem in getting Blob data and displaying
ResultSet problem
help with Tomcat
Error while using Servlets to call EJB i.e CMP