Solved

Servlet Error

Posted on 2002-04-30
17
261 Views
Last Modified: 2013-11-24
I have a quick question.
What is the difference between the following:
   String test = req.getParameter("Something").trim();
and
   String test = "";
   test = req.getParameter("Something").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
0
Comment
Question by:kmurphy7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
  • +4
17 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 6981593
listening...
0
 
LVL 5

Expert Comment

by:jamoville
ID: 6981652
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?
0
 

Author Comment

by:kmurphy7
ID: 6981663
test = ""

 If I am not mistaking there is a difference between "" and null. Is this correct.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 86

Expert Comment

by:CEHJ
ID: 6981669
You're correct
0
 
LVL 5

Expert Comment

by:jamoville
ID: 6981676
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("Something") 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.
0
 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 6981733
both are wrong. You want to use:

   String test = req.getParameter("Something");
   if (test != null)
      test = test.trim();
// else
//    test = "";  // maybe you want a default value if it's not specified
0
 
LVL 5

Expert Comment

by:jamoville
ID: 6981869
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.
0
 
LVL 3

Expert Comment

by:jerelw
ID: 6982034
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.
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6982119
> 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)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 6982129
>>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.
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 50 total points
ID: 6982134
is this the second example?
  String test = "";
  test = req.getParameter("Something").trim();

if so, it is not being assigned null, it is being assigned to a zero length string
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 6982154
Ah. It's becoming clear. Both I and bobbit31 are right:

>> it is not being assigned null, it is being assigned to a zero length string

>>Otherwise
the assignment has not worked.

The assignment never gets made, as req.getParameter("Something").trim(); throws an exception. Therefore 'test' holds its original value, i.e. a zero length string! Thanks for the prompt bobbit31
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6982158
no prob ;)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 6982168
kmurphy7 - we've solved the mystery I think!
0
 
LVL 35

Expert Comment

by:girionis
ID: 8682739
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8683207
hmmmmmmm.
0

Featured Post

Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

617 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