string stringbuffer append NULL strings

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?
LVL 2
PaullkhaAsked:
Who is Participating?
 
Mig-OCommented:
(req.getParameter("dude")==null?"":req.getParameter("dude")).trim()

This is the case since at least Servlet Api 2.2 AFAIK
0
 
ellandrdCommented:
what is req.getParameter("dude")
0
 
InteractiveMindCommented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
CEHJCommented:
A function such as the following will fix the parameter:

static String checkAppend(String s) {
             return s == null? "" : s;
}
0
 
Mig-OCommented:
It might have occured, because you didn't use a 100% Servlet API compatible Web Application Server before...
0
 
Jim CakalicSenior Developer/ArchitectCommented:
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
 
PaullkhaAuthor Commented:
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
 
Mig-OCommented:
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
 
Mig-OCommented:
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
 
PaullkhaAuthor Commented:
String y=null;
String z = y + "".trim();

was return empty string
now returning "null", length of 4
0
 
Mig-OCommented:
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
 
CEHJCommented:
>>
 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
 
anilbathulaCommented:
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
 
Mig-OCommented:
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
 
Mig-OCommented:
hust = just
0
 
aozarovCommented:
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
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.