?
Solved

string stringbuffer append NULL strings

Posted on 2005-04-08
17
Medium Priority
?
1,094 Views
Last Modified: 2012-05-05
String dude = req.getParameter("dude") + "".trim();
String dudelen = dude.length();
This code at one time would return an empty string , I am certain with a length of 0(empty string).
Some strange behavior is now occurring.

dudelen is returning len of 4 because of the following:
JAVA API
x = "a" + 4 + "c"
x = new StringBuffer().append("a").append(4).append("c")
                           .toString()

append: The characters of the String argument are appended, in order, to the contents of this string buffer, increasing the length of this string buffer by the length of the argument. If str is null, then the four characters "null" are appended to this string buffer.

I AM POSITIVE THIS WAS NOT ALWAYS THE CASE. Anyone know why this occurred?
0
Comment
Question by:Paullkha
  • 7
  • 3
  • 2
  • +5
17 Comments
 
LVL 16

Expert Comment

by:ellandrd
ID: 13737397
what is req.getParameter("dude")
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13737586
This is a Servlet..  req.getParameter("dude"); returns a String, but only if one is passed to the Serverl, for example:

http://domain.com/servlet?dude=theStringHere
0
 
LVL 3

Accepted Solution

by:
Mig-O earned 400 total points
ID: 13737893
(req.getParameter("dude")==null?"":req.getParameter("dude")).trim()

This is the case since at least Servlet Api 2.2 AFAIK
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 324 total points
ID: 13737902
A function such as the following will fix the parameter:

static String checkAppend(String s) {
             return s == null? "" : s;
}
0
 
LVL 3

Expert Comment

by:Mig-O
ID: 13737948
It might have occured, because you didn't use a 100% Servlet API compatible Web Application Server before...
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 13738275
Possibly because the form that posts the request to your servlet changed so that it no longer guarantees the form field named dued has a non-null value? If at one time it always guaranteed at least an empty string but now does not then your inputs would change, eh?
0
 
LVL 2

Author Comment

by:Paullkha
ID: 13739092
a little history
String dude = req.getParameter("dude") + "".trim();

req.getParameter("dude") - this will return a null string if "dude" does not exist.

+ "" will add an empty string

.trim() will trim the string

since parameter dude was null at times, .trim() would through an exception
to fix this exception, an empty string was appended to the return from req.getparameter("dude").
Now it appears that instead of getting a nice empty string when parameter "dude" is null, I get the string "null". This is bad, as it will likely break code.
0
 
LVL 3

Expert Comment

by:Mig-O
ID: 13739138
At all, you must assume that .getParameter(...) is able to return null. Reliying on any other behaviour will make it _your_ fault that the code breaks. Sorry.
0
 
LVL 3

Expert Comment

by:Mig-O
ID: 13739148
oh, there might an older jdk, where (null + "") threw a nullpointer exception. Just look through history of the current "Java Virtual Machine Specification", when they changed that.
0
 
LVL 2

Author Comment

by:Paullkha
ID: 13739160
String y=null;
String z = y + "".trim();

was return empty string
now returning "null", length of 4
0
 
LVL 3

Expert Comment

by:Mig-O
ID: 13739225
ups, i misunderstood you. But I would really really not believe, that is ever ever has been like that. Try to download an old jdk and please post the results.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13739419
>>
 String y=null;
String z = y + "".trim();

was return empty string
now returning "null", length of 4
>>

It will do. Try

String z = checkAppend(y);

as per my code
0
 

Expert Comment

by:anilbathula
ID: 13739676
String dude = req.getParameter("dude")

The req.getParameter("dude") will return a null string if "dude" does not exist and appending the null with "" will add an empty string returns null object.

0
 
LVL 3

Expert Comment

by:Mig-O
ID: 13739962
read this: http://java.sun.com/products/servlet/2.2/javadoc/javax/servlet/ServletRequest.html#getParameter(java.lang.String)

public java.lang.String getParameter(java.lang.String name)

    Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.

I suppose, you hust didn't encounter the case without the parameter before. I even swear it.
0
 
LVL 3

Expert Comment

by:Mig-O
ID: 13739965
hust = just
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13742292
I understand that you are not looking for a solution (CEHJ provided one for you), but rather is trying to understand why
it used to return empty string a while ago, right?
The link provided by Mig-O above is based on the 2.2 API and explains about the behaviour of getParameter.
I checked also the 2.1 spec and it seems the same: http://java.sun.com/products/servlet/2.1/api/javax.servlet.ServletRequest.html#getParameter(javax.servlet.String)
I didn't check 2.0 spec but based on this article http://www.javaworld.com/javaworld/jw-12-1998/jw-12-servletapi_p.html
there was not any change regarding the return value of getParemter call for non exsisting parameter.
Assuming you are right, leads me to beleive that maybe your web-container was not fully complient at the time and returned
empty string to such calls.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13875716
:-)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month16 days, 23 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question