• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.NullPointerException

 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

My main Index.java class


Then st.buttons(1,0) method:



displayButtons(active) method:


The tomcat OUTPUT:


In this method HttpSession session=req.getSession(); is throwing NPException. How can I fix it please?

Best regards
 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are first setting the request reference to null and then accessing the method getsession() on the req,i think thats why its throwing null pointer exception!
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
geet kaur wrote:you are first setting the request reference to null and then accessing the method getsession() on the req,i think thats why its throwing null pointer exception!


If I am removing null then it is throwing compile time error "variable req might have not been inititalized"
 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will generate a compiler error because you are declaring it as a local variable inside a method and then you are using it before initializing it.I did not get,why you are declaring it when you already have the req object?
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
geet kaur wrote:I did not get,why you are declaring it when you already have the req object?


I don't understand where I have the req object can you please correct my code?

I want to get

Best regards
 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey remove the HttpRequest req from there and then see does it work??
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
geet kaur wrote:Hey remove the HttpRequest req from there and then see does it work??


It says "can not find symbol req" at compile time
 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not actually getting the code that you have written??I am not getting what your code is exactly doing,and i think some expert on this would be of great help.
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Geet Kaur I am also waiting for any expert
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The request instance is created by the container and passed to the servlet when it is invoked as a parameter. You can't just conjure one out of thin air.
 
Souvvik Basu
Ranch Hand
Posts: 96
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Bear pointed out, you are not supposed to create a request object. An HttpServletRequest object is created by the container when a client requests your servlet. You just USE the object created by the container.
If removing the initialization (which is the right thing to do) is saying "req not initialised", it means there is no request object available for you to use in that part of the code.
Makes sense?
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks bear and Souvvik but still confused

Souvvik Basu wrote:As Bear pointed out, you are not supposed to create a request object. An HttpServletRequest object is created by the container when a client requests your servlet.


You mean when this method is called then calling method will invoke HttpServletRequest?

You just USE the object created by the container.

you mean I should change the code like this?


Please correct me if I understood wrong

Best regards


 
Souvvik Basu
Ranch Hand
Posts: 96
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Farakh,
If you recall the signature for doGet() or doPost() methods, this is how it mostly looks :


When your client sends in a request through the jsp, using a form, the container sees the method of the request, GET, POST etc. Suppose the method is GET. So container invokes the doGet() method in the servlet, and passes it the reference to the HttpServletRequest and HttpServletResponse objects that it created to handle this particular request. The container does not take responsibility for passing references of these objects to any other method that you have written yourself.

Hope this clears the confusion.
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for your help but if you check my code:
1) I am setting session Id while a client access the doGet/doPost method.
2) Then in another method within doGet I am checking that what Id is set in this session
3) And then doing something on basis of this session Id.

My question is how can I use HttpServletRequest without doGet or doPost? Please check my code and make changes as this will be very easily understandable what you are saying.

Best regards
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Farakh khan wrote:My question is how can I use HttpServletRequest without doGet or doPost?

You can't. Why would you want to?
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:You can't. Why would you want to?


1) Can you please specify why I can not use it without doGet/doPost?
2) I am coding one main center website and this website has many sub websites of its centers. I am putting check when the client access the domain of main website then I am checking either user has redirected from any center website or directly accessing. The logic behind this process is to forward proper static contents from database according to the center or main website ID. My doGet method sets ID and sub method is displaying records according to this session ID. This is working fine when am doing like this:

from doGet method I am doing:

I want to know why its not working independently

Best regards



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic