• 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

Null vs empty string

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys I need your help on this one. I know the difference between the 2, so that isn't the problem. I have a real simple html page with 1 textbox called sex and a submit button. Upon submit I call a JSP that just displays what is in the parameter "sex". I am sending the sex with nothing in it. I am storing this sex variable value in a bean and also printing out it's value using request.getparameter. My bean says the value is null while my request.getParameter says that it is empty string. Why? Which one is it? See code below. I have code before this that initializes the bean and stores the sex value in it.

 
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thomas Knight:


My bean says the value is null while my request.getParameter says that it is empty string. Why? Which one is it? See code below. I have code before this that initializes the bean and stores the sex value in it.




if your your request.getParameter says if is an empty string then it would be an empty string.
regarding your bean it depends how you are calling setter of bean and what is intial value of variable sex in bean.

use some javascript to debug your problem.

before submitting the forms check the following

alert(document.document.forms[0].sex.selectedIndex);
alert(document.document.forms[0].sex.options[document.document.forms[0].sex.selectedIndex].value);

and also print the length of

out.println(request.getParameter("sex").length());

hope you get something to find the answer of your problem
[ October 23, 2004: Message edited by: Shailesh Chandra ]
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't check for presence of ws chars. Don't ignore case on non-letter chars (ws/empty string).

 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do this,

out.println(request.getParameter("sex").length());
out.println(bean.getSex().length());

if got nothing, it means empty string.
if got null pointer exception then it means its null.
 
Thomas Knight
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help guys! It was as one of you had mentioned, it depends on what the variables in the bean are initialized to. I had not originally initialized them to anythin, so they were null. But then I initialized them to empty string so when they had no value in them they defaulted to empty string. Thanks again!
 
So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic