There are various ways to Avoid NullPointerException
1) if( null != objName ){ }
2) instead of .toString() method always use String.valueOf(objName);
3) Instead of of returning null collections object from the methods
try to return empty objects
return Collections.EMPTY_SET;
return Collections.EMPTY_MAP;
return Collections.EMPTY_LIST;
4) while comparing two objects using the obj1.equals(obj2) method
keep in mind that obj1 should be known and object2 should be unknown.
means in the argument of .equals() method always pass the unknown object
These are four simple basic ways. there are many more.
Keep Visiting :)
1) if( null != objName ){ }
2) instead of .toString() method always use String.valueOf(objName);
3) Instead of of returning null collections object from the methods
try to return empty objects
return Collections.EMPTY_SET;
return Collections.EMPTY_MAP;
return Collections.EMPTY_LIST;
4) while comparing two objects using the obj1.equals(obj2) method
keep in mind that obj1 should be known and object2 should be unknown.
means in the argument of .equals() method always pass the unknown object
These are four simple basic ways. there are many more.
Keep Visiting :)
No comments:
Post a Comment