*
The moose likes Servlets and the fly likes java.lang.NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "java.lang.NullPointerException " Watch "java.lang.NullPointerException " New topic
Author

java.lang.NullPointerException

Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
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

Joined: Sep 03, 2008
Posts: 78
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

Joined: Mar 22, 2008
Posts: 732
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

Joined: Sep 03, 2008
Posts: 78
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

Joined: Mar 22, 2008
Posts: 732
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

Joined: Sep 03, 2008
Posts: 78
Hey remove the HttpRequest req from there and then see does it work??
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
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

Joined: Sep 03, 2008
Posts: 78
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

Joined: Mar 22, 2008
Posts: 732
Yes, Geet Kaur I am also waiting for any expert
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61084
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Souvvik Basu
Ranch Hand

Joined: Apr 05, 2010
Posts: 96
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

Joined: Mar 22, 2008
Posts: 732
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

Joined: Apr 05, 2010
Posts: 96
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

Joined: Mar 22, 2008
Posts: 732
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

Joined: Jan 10, 2002
Posts: 61084
    
  66

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

Joined: Mar 22, 2008
Posts: 732
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



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.NullPointerException