• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java.lang.NullPointerException

 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is my jsp


this is my servlet


when i run this jsp and put any name and then it gives this ERROR?\

anyway i delete the cookies also?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This code assumes there will always be a "userName" - obviously this is a bad assumption.

Better check for sessionName == null before using it.

Bill
 
anarkali perera
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but my problem is how the it goes to the else part when i am using my first request?
when my first request it is a new session?
 
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey anarkali,

In jsp page, the default value of session attribute in page tag is true. Due to this session is automatically created as soon as you access the jsp.

In your code when you access the jsp page, the session is created. After that when you click on submit button, the request associated with the session is forwarded to the servlet. In servlet you check whether the session is new. As the session was created in jsp, it returns false and start the execution of else section.

If you update your jsp by setting session attribute in page tag. This issue would be resolved. Kindly try the following jsp and let me know if you still face any issues.

 
anarkali perera
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes.it is working.but problem is now each request is taking as a new user.it doesn't goes to else part.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

anarkali perera wrote:yes.it is working.but problem is now each request is taking as a new user.it doesn't goes to else part.


What are the changes you made? And what the output you are getting now? Does your request goes to the servlet correctly ?
 
anarkali perera
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is my jsp now


this is my servlet


yes.it directly goes to servlet.is it bad?
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

With this in JSP you don't have a session associated with it so the request goes to the servlet from the rendered HTML by the JSP, in servlet a new session will be created.
 
anarkali perera
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then how do i change this to when the second request comming it goes to else part?
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once you have created the session in the servlet (for the first request) or use the existing one (on subsequent requests) you have the session associated with that particular client. You don't have to do anything other than what you have done in the servlet to make the session work. Try sending the response from the servlet (by forwarding to JSP) and the next request from that will use the existing session if you check that in the servlet. There can be many ways to test this.
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yes.it is working.but problem is now each request is taking as a new user.it doesn't goes to else part



Servlet would consider only the first request from jsp as a new user. As soon as the request hit servlet, a new session would be created and all further communication would happen with the same session until you close the browser.

Kindly update the doPost in following way and follow the below given steps in sequence, you yourself would see the same session working

1- go to the jsp and enter a value in text box and click on submit
2- you would see "NEW USERRRR" in the browser.
3- Click on back button of the browser and then enter the same value that you entered last time and click on submit
4- you would see YOU ARE OLD USER.
5- Click on back button of the browser and then enter the different value than you entered last time and click on submit
6- you would see "you are a old user with different name" in the browser.

Using this scenario, you would see all the possible way of executions.



 
anarkali perera
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i change the servlet to this.but it gives nothing
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should forward the request & response to the defined resource in the RequestDispatcher. Add this after "re" initialization:

 
anarkali perera
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok.i change to this


but still print nothing
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should provide more details, How are you invoking the servlet (as you are directly invoking it now) and the mappings in web.xml ? Also take look at RequestDispatcher, ServletRequest & ServletContext for more details.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't forward the request after sending something to the response. So either remove the p.println statements or remove the forwards.

Again you are not checking if there is a request parameter named name or not. Also if the session is not new and the name in the session is not the same as request parameter name, then you are not sending anything as output...
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey anarkali,




In the above code, you would never see the effect of p.println("NEW USERRRR"); reason being at the end you are forwarding the request to the jsp. so the already existing contents would be cleared while forwarding the request to the jsp


I would recommend using the below code in post method instead...



Try this code and execute the code and let me know if you still face any issues.
 
anarkali perera
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this also print nothis machang.because it forward the request to the form.jsp to before printing.
it is ?
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

anarkali perera wrote:....because it forward the request to the form.jsp to before printing.it is ?


You can either do the printing in the servlet or use the JSP to render what you want. In this case it is the latter.
 
Whatever you say buddy! And I believe this tiny ad too:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic