• 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 String vs String "null"

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I searched many places but could not find anything related to my problem.

Here is the problem:
A jsp has a parameter named, say, power, in its form.


Upon POST, a servlet gets the value via


However, if the parameter "power" happens to be a null string (as depicted in above code), then mypower is not null, but it is a true String with value "null".

This is odd, what if the power is indeed "null"? How do I know which is null and which is "null"? (Unless I did something wrong, of course.)

Thank you,
Ben
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ben Zhang ,

null is a key word used to initialize variables. In your case as you have declared the power variable and initialized it to null, mypower will have the same value. The difference between null and "null" is the later is a specific value similar to"xyz" and the earlier means it has no value, simply blank. If you try to access the earlier one (null) you will get a "null pointer exception". But there will be no issue in accessing the later one ("null"). To distinguish between these two simply compare them with null.

ex: String s1 = null;
String s2 = "null";
if(s1==null)
{ do something }
Hope this will clarify you.
 
Ben Zhang
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Krishna,

In my case, I want null, but I get "null".

Thanks,
Ben
 
Gopikrishna Kunisetty
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ben,

Here is what actually happens. When you assign your variable(powerVal) value to the hidden parameter, whatever the value it has, the hidden type parameter treats it as value. Though you have declared powerVal and initialized it to null, it becomes a proper non-null string value for the hidden parameter.
Instead, what you can do is, simply put your variable into the request scope using setAttribute method and retrieve it wherever you want. This will give you the desired result.
[ September 24, 2008: Message edited by: Gopikrishna Kunisetty ]
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All HTTP request parameters values are strings. If you look at the HTML source that is being displayed in the browser for your form, you will see that the value of the hidden element (in addition to not being properly quoted) is the string null.

The only time you'll get a true null from getParameter() is when the named element does not exist.
 
Ben Zhang
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Krishna, using Attribute is the solution.

Thanks Bear for the explanation.

Just my opinion, the behaviour that JSP/Servlet secretly change something under the hood makes debugging quite difficult. Could just error out if null hidden values are not allowed. Could it be container dependent? I am also quite surprised that I could not find people talking about this on the net.

Thanks guys,
Ben
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Zhang:
Just my opinion, the behaviour that JSP/Servlet secretly change something under the hood makes debugging quite difficult.

Nothing got "changed under the hood". It's doing exactly what you told it to do. And it has nothing to do with JSP or servlets.

Could just error out if null hidden values are not allowed.

Any text value is allowed. How would it "error out" on something that's completely valid? How do you expect a true null to be placed in an HTML page? How do you expect it to be expressed in a text-oriented protocol like HTTP?

I am also quite surprised that I could not find people talking about this on the net.

There's nothing to talk about. The first T in HTTP stands for text. No one expects it to do anything differently.
[ September 24, 2008: Message edited by: Bear Bibeault ]
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just so you have a reference to see where this all comes from... The JSP uses a JSPWriter to output data. In the API for the JSPWriter#print(String) method it tells you that any null value will be translated to "null". So this is specified in the API, though it would take some search to know it.
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One could argue that having a JSP scriptlet expression that evaluates to null emit the string "null" is sorta stupid. And I'd probably agree. The EL fixes this issue by emitting nothing.

So in this example, using the EL you'd get back the empty string. But there's still no way to get back a Java null -- that's simply not a concept that's expressible using HTTP.

If you want a null, don't create the element in the first place.
 
Ben Zhang
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Steve for the reference, which clears the issue with official source.

I don't like the way it is now, but at least I have some black magic to work with, Attribute (Krishna) and EL+empty string (Bear).

Thanks for the help,
Ben
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't really care for the attribute solution. By using it, your data is coming from two sources: the form, and the stored session scoped variable (the correct term for "attribute"). This is architecturally fragile.

I'd prefer to see you use the form as the only data conduit. You can either omit the hidden variable when the value would be null, and hence getParameter() would return the Java null, or use the EL (much preferably to putting Java code in JSPs) and interpret the empty string accordingly.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic