• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Problem in using date in WHERE clause in SELECT query in Servlet.

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends!
I am new in this Forum and I have logged here for my problem.
I am developing a online system for applying for a post for my client.
But the problem is when I use 'date' in 'WHERE' clause in 'SELECT' query in servlet which is used to allow user to re-print the registration page, it is giving null pointer exception.
In this when I fill correct information it is giving null pointer exception otherwise result is 'records not exists'.
I am pasting all the codes, the print.java servlet and also print.html file for better help.

print.html


----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
now print.java



in my table reg_no is bigint(40) and dob is of 'date' type. I am using mysql and xampp.
Please help me, you can check this code by implementing this code, and then date you have entered and regno in the db,use exactly that in print.html and run this code, it will give you error null pointer exception, and if you use only regno then it is running without error. and if you use regno with wrong date then again it is running without error and give 'no record find' I am not understanding what is the problem in 'date' whenever i am using right value of 'date' it is giving error.
Please help me.
 
Sarthak Gupta
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what do you mean by that!
 
Ranch Hand
Posts: 68
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please use code tag for code.
 
Sheriff
Posts: 28394
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sarthak Gupta wrote:what do you mean by that!



That means: Just because your code happens to be in a servlet, that doesn't make it a servlet problem.

You've probably got one of the standard JDBC problems. Unfortunately your code isn't in the code tags, so it's very hard to read. There's also a lot of code there which doesn't even have anything to do with Java, which just makes it harder to locate your problem.

Anyway what you have to do is to look at the stack trace. It should tell you exactly what line of code threw the exception, and then you can start looking at that line to see what variable is unexpectedly null. You do have a stack trace, don't you?
 
Sarthak Gupta
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know Java has built-in stack trace but how to use that, I don't know.
Please guide me.
 
Sarthak Gupta
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Neeraj Dhiman wrote:please use code tag for code.



Please guide me how to use code tag.
I'm new to this Forum.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sarthak, and welcome to the Ranch!

I've added the code tags to your post, so that you can see how does that look like. See UseCodeTags (<-- click) to learn how to use them on this site.

You're using e.printStackTrace() in your catch block, which is fine - that way the stack trace should be written to a log somewhere on your web server. I don't know xampp myself, so I cannot tell you where you'll find it, but you might try to find it out in xampp documentation. Find the stack trace in your logs and post it here.
 
Sarthak Gupta
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Martin Vajsar wrote:Hi Sarthak, and welcome to the Ranch!

I've added the code tags to your post, so that you can see how does that look like. See UseCodeTags (<-- click) to learn how to use them on this site.

You're using e.printStackTrace() in your catch block, which is fine - that way the stack trace should be written to a log somewhere on your web server. I don't know xampp myself, so I cannot tell you where you'll find it, but you might try to find it out in xampp documentation. Find the stack trace in your logs and post it here.



Thank you for your support Martin.

the error is(tracked form printstacktrace)- java.lang.NullPointerException

Whenever I use the value which is stored in DB it is giving error otherwise it is display no record found.

I think I must post this to JDBC Forum.
 
Sarthak Gupta
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey when I checked in JDBC Forum, my thread is already there, so now for all JDBC problem solvers-
please solve my also.

When ever I run the above code I got error(which I wrote just above this post), please help me
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, this thread already is in the JDBC forum. One of the moderators here moved your post when he noticed it is indeed a JDBC related problem.

Did you actually find the stack trace in your logs? In this case, there will be not only the exception name (NullPointerException), but also the line number on which the exception occurred. This would help tremendously to track down the problem. It is much more probable that the "JDBC solvers" will help you if you provide this information (see also TellTheDetails).

In any case, take a closer look at this code fragment of yours:

There are several problems with it:

1) If any of these database fields is NULL, you'll get a NullPointerException, as you cannot use toString() with nulls.

2) This code loops over all records returned by the query, overwriting the values in your variables in each go without actually using them. Only the values from the last record are kept in the variables and used by the later code. This is suspicious and even if it was a desired functionality, it should be done differently.

3) Though you close the resources (rs, stmt, con), you don't do this in a finally block, therefore they don't get closed when an exception occurs.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic