Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Null vs empty string  RSS feed

 
Thomas Knight
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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.

 
Shailesh Chandra
Ranch Hand
Posts: 1084
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • 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 ]
 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't check for presence of ws chars. Don't ignore case on non-letter chars (ws/empty string).

 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!