Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Resolve NullPointerException

 
Singh Anisha
Ranch Hand
Posts: 100
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Throwing following exception
type Exception report

message

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

exception

java.lang.NullPointerException
LogIn.doPost(LogIn.java:21)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please edit your post to UseCodeTags, then indicate to us which line is line 21, and also tell us which object is null.

What are the "toString()" calls supposed to accomplish for username and password?

Strings are not compared using the "==" operator, but using the "equals" method.

The DB query won't work because you're using the string "username" where you should be passing in the value of the "username" variable. Read up on PreparedStatement on how to do that.
 
Singh Anisha
Ranch Hand
Posts: 100
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String username=request.getParameter("username");

Here to fetch form field data of username, and then put it to variable username.

Exception is thrown at line 15 of above code.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exception is thrown at line 15 of above code.

That seems unlikely. The only object that could cause such an exception would be "response" - which was NOT null in line 14.
 
Singh Anisha
Ranch Hand
Posts: 100
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i rearranged the codes and find problem is in SQLpart


Now browser displaying...
Query problem
Failed..
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use out.write("Query Problem: "+e.getMessage()) instead so that you'll know what the problem actually is.
 
Singh Anisha
Ranch Hand
Posts: 100
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Illegal operation on empty result set.
 
Paul Clapham
Sheriff
Posts: 21307
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right, you do need to consider the possibility that your query will return zero records. You should call the "rs.next()" method before trying to use any data from the first row -- after all, there might not be one. The "next" method will tell you that.
 
Singh Anisha
Ranch Hand
Posts: 100
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all of you. Finally now no exceptions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic