• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Why am i getting NullPointerException but runs properly

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Sheriff
Posts: 67590
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear said something about checking it for null.
 
Gaurav Wadhwani
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
reply
    Bookmark Topic Watch Topic
  • New Topic