kmurphy7
asked on
Servlet Error
I have a quick question.
What is the difference between the following:
String test = req.getParameter("Somethin g").trim() ;
and
String test = "";
test = req.getParameter("Somethin g").trim() ;
If Something is not in the querystring I get an error.
For some reason when i use the first example I get a nullpointer exception. If I use the second example it works with out a problem. I am using JRun 2.3.
Thank You in advance.
Kent
What is the difference between the following:
String test = req.getParameter("Somethin
and
String test = "";
test = req.getParameter("Somethin
If Something is not in the querystring I get an error.
For some reason when i use the first example I get a nullpointer exception. If I use the second example it works with out a problem. I am using JRun 2.3.
Thank You in advance.
Kent
listening...
When you run the second example and "Something" is not in the querystring (the one you indicate works ok) what is the value of test?
ASKER
test = ""
If I am not mistaking there is a difference between "" and null. Is this correct.
If I am not mistaking there is a difference between "" and null. Is this correct.
You're correct
I'm not using JRun but I get a null pointer for both which is what you would expect to get. There is a difference between "" and null. The line req.getParameter("Somethin g") should be returning null. When the trim() is applied to the null value returned you should get a null pointer exception. This is what I'm seeing, but maybe JRun behaves differently.
both are wrong. You want to use:
String test = req.getParameter("Somethin g");
if (test != null)
test = test.trim();
// else
// test = ""; // maybe you want a default value if it's not specified
String test = req.getParameter("Somethin
if (test != null)
test = test.trim();
// else
// test = ""; // maybe you want a default value if it's not specified
jlouwere :
The code you wrote would work but why kmurphy7 is not getting a null pointer exception with the second example is the real question that should be answered.
The code you wrote would work but why kmurphy7 is not getting a null pointer exception with the second example is the real question that should be answered.
I don't believe that the code posted is the actual sequence of events.
please post the actual block of code with the issue, and we can see when and where values are being set and if the code is in a try - catch.
please post the actual block of code with the issue, and we can see when and where values are being set and if the code is in a try - catch.
> why kmurphy7 is not getting a null pointer exception with the second
example
b/c it is not a null object... it is a String object of zero length (like an empty vector for example)
example
b/c it is not a null object... it is a String object of zero length (like an empty vector for example)
>>b/c it is not a null object...
It *should* be after it has been assigned null (which is the case in the second example). Otherwise the assignment has not worked.
It *should* be after it has been assigned null (which is the case in the second example). Otherwise the assignment has not worked.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
no prob ;)
kmurphy7 - we've solved the mystery I think!
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
- points to bobbit31
Please leave any comments here within the
next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !
girionis
Cleanup Volunteer
I will leave a recommendation in the Cleanup topic area that this question is:
- points to bobbit31
Please leave any comments here within the
next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !
girionis
Cleanup Volunteer
hmmmmmmm.