• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

JSP Question - URGENT

Ok, the following is my question.  Say I have a class implementing a tag.  This tag has two attributes, both correctly defined in the tld and correctly used in the JSP.  If the class implementing the class has a method like this:

public String getFirstAttr() {
 return firstAttr + getSecondAttr();
}

If we assume everything is correctly implemented, is it possible for getFirstAttr to return the value of firstAttr concatenated with null instead of the value provided for secondAttr?

Please respond quickly.

John Rayburn
0
Laminamia063099
Asked:
Laminamia063099
  • 2
  • 2
1 Solution
 
blackCommented:
yes if getSecondAttr() returns null and firstAttr is equal to "hello" you'll get the following string:
hellonull
basically the + operator when used with strings will convert any null reference to the string "null" and that string literal will be used.
It might be best if you did
public String getFirstAttr() {
  if(getSecondAttr == null) {
    return firstAttr;
  }
  return firstAttr + getSecondAttr();
}

that way the string "hello" will be returned instead of "hellonull"
0
 
Laminamia063099Author Commented:
Notice, I mention that getSecondAttr returns null "instead of the value provided for secondAttr". If secondAttr is not null, null would not be returned, correct?

John
0
 
blackCommented:
yes, if the getSecondAttr method returns a non null value then you will not get null concatenated.
0
 
Laminamia063099Author Commented:
Thanks.  I was reading a book passage that was incorrect and I wanted a second opinion!

John
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now