Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

curious case of JSP page passing empty string,instead of passing null value

 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am facing this peculier case. I have written two files inde.jsp and hello.jsp. Index basically passes a trribute userName to hello.jsp.
See the code

index.jsp:


hello.jsp:


When I start with index.jsp. and don't enter anything in textfeild, I am sending a empty string to hello.jsp. When I remove the commented line at it prints "User name is 0". Instead of throwing NullPointerException. Is this problem some vendor specific thing.

And also when I directly go for hello.jsp it throws NullPointerException.

I am unable to make head and tail of the execution. Please help me.

Anil Deshpande
 
Bartender
Posts: 4107
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

.... and don't enter anything in textfeild, I am sending a empty string to hello.jsp. When I remove the commented line at it prints "User name is 0" ....



What's wrong with it? What's the length of an empty String variable?

And also when I directly go for hello.jsp it throws NullPointerException.


That's because you don't have a request parameter called "userName".
 
Anil Deshpande
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't you think, it is wrong. When I am not entering anything in the text feild, I expect it to be a null value. not a empty String with length=0.

I will never be sure whether to check for empty string or handle NullPointerException.

Or may be I will have to check both conditions............
 
Vijitha Kumara
Bartender
Posts: 4107
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anil Deshpande wrote:I will never be sure whether to check for empty string or handle NullPointerException.
Or may be I will have to check both conditions............



What ever user enters there you'll get as a String (empty or not). It's up to you to decide what you want to do with that value.
 
Anil Deshpande
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What ever user enters there you'll get as a String (empty or not).



So will it defnitly return empty String in case i don't enter anything in text feild.

If that is the case then no problem,

thanks a lot
 
Vijitha Kumara
Bartender
Posts: 4107
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anil Deshpande wrote:So will it defnitly return empty String in case i don't enter anything in text feild.



Yes. It should.
 
expectation is the root of all heartache - shakespeare. tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic