?
Solved

Advantage of using a has content method, java

Posted on 2007-10-04
3
Medium Priority
?
311 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]

0
Comment
Question by:manojpotla
  • 2
3 Comments
 
LVL 92

Expert Comment

by:objects
ID: 20013045
no, what you are doing is required as the NPE exception will get thrown before hasContent() gets called.
0
 

Author Comment

by:manojpotla
ID: 20013109
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
0
 
LVL 92

Accepted Solution

by:
objects earned 750 total points
ID: 20013137
i'd go with what you are already doing. the suggested code is confusing.
or refactoring it a little to look like:

LCSCycle cdescription = (LCSCycle)productRevA.getValue("cdescripton");
if(SomeHelper.hasContent(cdescription)){
    String cnumber = (String)cdescription.getValue("cnumber");
    if(FormatHelper.hasContent(cnumber){
        ..
    }
}
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses

621 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