Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why am i getting NullPointerException but runs properly

 
Gaurav Wadhwani
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Nikhil Pasupukuntla
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stefan Evans
Bartender
Posts: 1640
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2851
10
Fedora Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear said something about checking it for null.
 
Gaurav Wadhwani
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic