Topic : Avoid Null Pointer Exception
Advantage
Code Execution will be liberal and better. Functionality break chances will be reduced.
Example
1)
if ("check me".equals(param)) // Do like this
{
// some code
}
2)
String str = (param == null) ? "NA" : param;
3)
Use String.valueOf() Rather than toString()
4)
Object object =null;
String.valueOf(object); /// is OK
String.valueOf(null); // throws Exception
Use collections default to get the empty objects of respected data struture
List<String> list = Collections.EMPTY_LIST;
Set<String> set = Collections.EMPTY_SET;
Map<String,String> map = Collections.EMPTY_MAP;
knownObject.equals(unknownObject)
Advantage
Code Execution will be liberal and better. Functionality break chances will be reduced.
Example
1)
if ("check me".equals(param)) // Do like this
{
// some code
}
2)
String str = (param == null) ? "NA" : param;
3)
Use String.valueOf() Rather than toString()
4)
Object object =null;
String.valueOf(object); /// is OK
String.valueOf(null); // throws Exception
Use collections default to get the empty objects of respected data struture
List<String> list = Collections.EMPTY_LIST;
Set<String> set = Collections.EMPTY_SET;
Map<String,String> map = Collections.EMPTY_MAP;
knownObject.equals(unknownObject)
No comments:
Post a Comment