We help IT Professionals succeed at work.

Advantage of using a has content method, java

manojpotla
manojpotla asked
on
355 Views
Last Modified: 2013-11-23
Hi
In my application there is a part of code which is as follows
[code]
1. if(FormatHelper.hasContent((String)productRevA.getValue("Style Num")){
    String stylenum = (String)productRevA.getValue("Style Num");
}
2. LCSCycle cdescription = (LCSCycle)productRevA.getValue("cdescripton");
    if(FormatHelper.hasContent((String)cdescription.getValue("cnumber")){
    String cnumber = (String)cdescription.getValue("cnumber"))
}
[/code]
The problem here is cdescription when is being initialized as LCSCycle can be null, so there would be an error in the code in point 2. To avoid it i have re-written the code as
[code]

LCSCycle cdescription = (LCSCycle)productRevA.getValue("cdescripton");
if(cdescription != null){
    if(FormatHelper.hasContent((String)cdescription.getValue("cnumber")){
    String cnumber = (String)cdescription.getValue("cnumber"))
    }
}
[/code]
But a co-worker of mine suggested that i may use a hasContent method rather tahn going for checking if the object is null. Is there an advantage of using a hasContent method in general. Thanks in advance for your suggestions.

Manoj


[/code]

Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
no, what you are doing is required as the NPE exception will get thrown before hasContent() gets called.

Author

Commented:
Hi
Thanks for your suggestion but here is what my co-worker suggests is to use a class which has a hasContent method like this

[code]

if(SomeHelper.hasContent((LCSCycle)productRevA.getValue("cdescripton")){
     LCSCycle cdescription = (LCSCycle)productRevA.getValue("cdescripton");
    if(FormatHelper.hasContent((String)cdescription.getValue("cnumber")){
    String cnumber = (String)cdescription.getValue("cnumber"))
    }
}
[/code]

Does it cause problems if i do not have a hasContent() at some point at a higher level?  

Thanks
Manoj
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.