| Author |
Why am i getting NullPointerException but runs properly
|
Gaurav Wadhwani
Ranch Hand
Joined: Sep 21, 2010
Posts: 68
|
|
What i am trying to make is a quiz. For Fetching the records from the same location from where the user quits, I need to maintain a session. So i am using a attribute "nq" which increments each time a question is answered. However, the initial value of nq will be null so i change it to 1 to fetch the first record. And increment it then on.
I get NullPointerException. If i get it only the first time I execute its understandable because of the above reason but If I close and start again even before session expires, i get it again. Why so?
There should be no NullPointerException again as it can fetch the value of nq during the session.
Also am i doing this the right way?
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56192
|
|
You could test it for null, and set it to some initial value if so.
P.S. Do you really think that "nq" is a good name for anything?
P.P.S. Instance variables in a servlet? Generally bad news!
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Nikhil Pasupukuntla
Greenhorn
Joined: Jun 04, 2009
Posts: 21
|
|
Hi Gaurav,
If you are using any IDE like eclipse or NetBeans you can debug your program by adding NullPointerException breakpoint and see where the problem is.
|
Nikhil Pasupukuntla
SCJP 1.5 | OCPJWCD 1.5
|
 |
Stefan Evans
Bartender
Joined: Jul 06, 2005
Posts: 1005
|
|
Gaurav Wadhwani wrote:If i get it only the first time I execute its understandable because of the above reason
but If I close and start again even before session expires, i get it again. Why so?
What defines a session?
Most often it is stored in a session cookie which is lost when you finish the browser session.
So closing the window and starting again issues you a new session, even if the old one is still active, because it knows nothing about the old one.
|
 |
Gaurav Wadhwani
Ranch Hand
Joined: Sep 21, 2010
Posts: 68
|
|
Sorry for the very late reply everyone.
@Bear Bibeault
Do you really think that "nq" is a good name for anything?
Yes its a dumb name...only for testing purposes.
Instance variables in a servlet? Generally bad news!
Yes, and what i want in my application is to give one DB connection to every user but i dont know how to give a DB connection in a session. Please help
@Nikhil Pasupukuntla
I learnt how to add a breakpoint but not how to test it. Please help.
@Stefan Evans
If the session cookie is lost on closing and restarting then how is it possible that i get to continue with the same question i left at. If i close on question 4 -> close and start it again i get question 5 . So it has session and the cookie available why then a null pointer Exception?
|
 |
Amit Ghorpade
Bartender
Joined: Jun 06, 2007
Posts: 2552
|
|
Bear said something about checking it for null.
|
SCJP, SCWCD.
|Asking Good Questions|
|
 |
Gaurav Wadhwani
Ranch Hand
Joined: Sep 21, 2010
Posts: 68
|
|
hmm, That what i did now and found the error.
It was line 95 , it had to be
What a time waste, kept thinking there is some logical error and it was some typing mistake. Anyway finally found and learned a lesson Thank you guys
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
Gaurav Wadhwani wrote: What a time waste, kept thinking there is some logical error and it was some typing mistake. Anyway finally found and learned a lesson  Thank you guys
This happens many of the times..and it shooks our head..by thinking where is the mistake...
|
SCJP6.0,My blog Ranchers from Delhi
|
 |
 |
|
|
subject: Why am i getting NullPointerException but runs properly
|
|
|