Monday, October 27, 2014

Java Coding Care4 : Avoid Null Pointer Exception

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)

No comments:

Post a Comment

Scrum and Scrum master

Scrum  Scrum is a framework which helps a team to work together.  It is like a rugby team (the scrum name comes from rugby game). Scrum enco...