View Single Post
Old 08-05-2003, 04:34 PM   #16 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
"i accidently get NullPointerExceptions when checking
what strings equal, how can i avoid it easily?":

Code:
String a = null;
if(a.equals("Hello")){ // oops exception!
}

// this may be solved with:
if(a != null && a.equals("Hello"))

// but you can just do:
if("Hello".equals(a)){ // and avoid checking null at all.
It is good practice to put the constant value on the
left-hand-side so accidents dont occur in other languages
(javascript, c, c++) where they evaluate assignment to
be either true or false and hence a valid expression in
an "if" statement.
__________________
direct entry file specification.
npa is offline   Reply With Quote