Java NullPointerException in if statement -
i nullpointerexception in following code.
else if (isempty(href)) system.err.println("this empty link: " + href);
where isempty function following.
public boolean isempty(string href){ if (href.equals("") || href.equals("null") || href == null || href.isempty()) return true; else return false; }
is because can not compare string null? can make function work?
your isempty
test doing things in wrong order. change this:
if (href.equals("") || href.equals("null") || href == null || href.isempty()) return true;
to:
if (href == null || href.equals("") || href.equals("null") || href.isempty()) return true;
if href
null
, first test short-circuit rest of if
, won't npe.
(by way, explicitly testing href.equals("")
, calling href.isempty()
redundant. need 1 or other.)
by by-the-way, recommend, matter of style simplifying method single return
statement:
public boolean isempty(string href){ return href == null || href.isempty()) || href.equals("null"); }
(also, if testing against string "null"
attempt check against null
value, can drop condition well. in fact, use apache commons' stringutils.isempty(charsequence)
method want.)
Comments
Post a Comment