java - May I get Null pointer exception from this routine? -
i have written below routine in java, need know code null pointer safe or not:
public class trm_fields { public static string returnactualvalue(string staffing_event, string currentvalue, string priorvalue) { string returnvalue; returnvalue = null; if ("trm".equalsignorecase(staffing_event) && currentvalue == null && priorvalue != null && !"".equalsignorecase(priorvalue)) { returnvalue = priorvalue; } else { returnvalue = currentvalue; } homecoming returnvalue; } } any of parameter staffing_event, currentvalue , priorvalue may null.
if not null pointer safe should accomplish that?
your method safe. correctly using "constantstring".equals(someobject) ensure null-safe comparison.
some other comments:
your method hard read because using titlecase java variables, when should camelcase.
you have 2 possible homecoming values. can simplify method follows:
public static string returnactualvalue(string staffingevent, string currentvalue, string priorvalue) { if ("trm".equalsignorecase(staffingevent) && currentvalue == null && priorvalue != null && !"".equalsignorecase(priorvalue)) { homecoming priorvalue; } else { homecoming currentvalue; } } note else build isn't necessary, it's matter of style whether include construction or have return currentvalue;.
java nullpointerexception
No comments:
Post a Comment