File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Null Pointer Exception in JSP servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Null Pointer Exception in JSP servlet" Watch "Null Pointer Exception in JSP servlet" New topic
Author

Null Pointer Exception in JSP servlet

Anurag Dhiman
Greenhorn

Joined: Feb 10, 2012
Posts: 26
Hello Everyone!

I am working on a web application using Jsp/Servlet. After successful login into application(eg URL changes to login.do its right) when I refresh the URL A NULL POINTER Exception is generated.
On simple right click refresh its works well, but on selecting url and pressing enter above exception generated. I am using hidden fields in jsp, /do in web.xml

Please help me to rectify above problem



Thank you!
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Can we see what you have written so far? Use code tags. And welcome to the ranch. :)


~ Mansukh
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2712
    
    5

Anurag Dhiman wrote:On simple right click refresh its works well, but on selecting url and pressing enter above exception generated.

It would depend on the particular browser what happens when you right click and select refresh. It may not be requesting the server.
See the server logs when you select refresh and when you hit refresh, you will see the difference.

SCJP, SCWCD.
|Asking Good Questions|
Anurag Dhiman
Greenhorn

Joined: Feb 10, 2012
Posts: 26
My Login Code is

<form action="login.do" name="loginForm"
onsubmit="return Blank_TextField_Validator()" method="post">

<table>

<tr>
<td></td>
<td></td>
</tr>

<tr>
<td>
<input type="hidden" name="screen" value="LoginPage" />
</td>
</tr>

<tr>
<td>
<font color="#613B24" size="4" face="candara"><b>User
Name</b>
</font>
</td>
<td>
<input type="text" class="paddingtop" name="loginid" id="loginid" />
</td>
</tr>
<tr>
<td>
<font color="#613B24" size="4" face="candara"><b>Password</b>
</font>
</td>
<td>
<input type="password" name="passwrd" id="passwrd" />
</td>
</tr>
<tr>
<td></td>
<td>
<input name="login" type="submit" class="button" value="GO" />
</td>
</tr>
</table>
</form>


And this is the Servlet Code

public class ActionController extends HttpServlet{
@Override
public void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

//Login Code Start
String screen=req.getParameter("screen");


HttpSession session= req.getSession(true);
java.util.Date d=new java.util.Date();

System.out.println("SCREEN " +screen);
if(screen==null){



rd=req.getRequestDispatcher("adminLogin.jsp");

rd.forward(req, resp);

}

//admin Login

if(screen.equals("LoginPage")){ // In this line its giving null pointer exception after url refersh
Date cdate=new Date(new java.util.Date().getTime());
System.out.println("DATE=" +cdate);
String username=req.getParameter("loginid");
String passwd=req.getParameter("passwrd");
System.out.println("User Name=" +username);
System.out.println("Password=" +passwd);

UserModel um =new UserModel();

uto=um.verifyUser(username, passwd);
if(uto.getLoginid()!=null ){
System.out.println("Login ID" +uto.getLoginid());
Timestamp t =new Timestamp(d.getTime());
CreateModel cr=new CreateModel();
session.setAttribute("USER", uto);
System.out.println("ANURAG");
//msg = "<font color='#58341E'> <b>Welcome USER " + uto.getLoginid()+ "</b><br/><br/></font>";
req.setAttribute("MSG", msg);
rd=req.getRequestDispatcher("userHome.jsp");
rd.forward(req, resp);

}
else
{
System.out.println("User Block");
msg = "<font color='RED' size='1' face='candara'><b><center>Wrong Username and Password</center></b></font>";
req.setAttribute("MSG", msg);
rd=req.getRequestDispatcher("adminLogin.jsp");
rd.forward(req, resp);
}
}
Rafael Prado Oliveira
Greenhorn

Joined: Apr 30, 2013
Posts: 28
Please use code=java /code tags..

its likely the nullpointer is being thrown by your action class, also paste you stacktrace

Please share it, and we will help


Have you debuged it? its a life savier!!


Another edit..

Seems when you reload the page the screen parameter is NULL, so null cannot be compared, because it does not know where(which memory address) to look for this value.

You can do it the other way arround it, compare something that you know will never be null to something that can be null.

Anurag Dhiman
Greenhorn

Joined: Feb 10, 2012
Posts: 26
Thank you for your comments

Below is the stack Trace


Code=JAVA code

SCREEN null
May 2, 2013 8:50:40 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet com.teknika.servlet threw exception
java.lang.NullPointerException
at com.teknika.servlet.ActionController.service(ActionController.java:58)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
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:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)


The ActionController.java:58 is "if(screen.equals("LoginPage"))"
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18135
    
    8

So your "screen" variable is null.

You're going to tell me that you checked for that already, aren't you? Well, you did, and if it is null (which it is) then you call the forward method, and then control continues on to the code after the if-statement. Here you use the "screen" variable again, and it's null, so you get an exception.
Anurag Dhiman
Greenhorn

Joined: Feb 10, 2012
Posts: 26
Ok Thanks!


Can you tell me any quick solution for that?
Rafael Prado Oliveira
Greenhorn

Joined: Apr 30, 2013
Posts: 28
you have two cases, if its null you want it to do something, and only if its diferent from null you want proceed and do something ELSE, right?
Anurag Dhiman
Greenhorn

Joined: Feb 10, 2012
Posts: 26
Yes you are right.

What I mean is If its null it will stay on the same page or redirects to Login page (any one)
Otherwise if its not null then proceed further
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18135
    
    8

Just to re-emphasize what Rafael said, are you familiar with the "else" part of the Java "if" statement?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null Pointer Exception in JSP servlet
 
Similar Threads
getting null pointer exeception
Apache with Tomcat integration Passing Authenticated values
Null pointer exception at _jspx_th_html_form_0.doStartTag()
Implicit session object becomes null in JSP
Passing a String[] from jsp to servlet