Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null Pointer Exception in JSP servlet

 
Anurag Dhiman
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we see what you have written so far? Use code tags. And welcome to the ranch. :)
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anurag Dhiman
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Thanks!


Can you tell me any quick solution for that?
 
Rafael Prado Oliveira
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to re-emphasize what Rafael said, are you familiar with the "else" part of the Java "if" statement?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic