dcsimg
 

Submitted by: M S Sridhar(sridhar.ms@gmail.com)

Tuesday Jan 2nd 2018 by M S Sridhar

Language: Java, Level: Intermediate: - Avoiding null pointer exceptions is a good programming practice. Learn some more ways to avoid this pitfall.

Avoiding null pointer exceptions is a good programming practice. However, simple things such as string comparison can also lead to null pointer exceptions.

Considering the following:

You have a constant string and a variable string to be compared. There are 2 ways of checking their equality, however the variable string can be null in both the cases.

Case 1: variableString.equals(constantString)
Case 2: constantString.equals(variableString)

In the above cases, case 1 can lead to a null pointer exception if the variableString is null. However, in case 2 it does not lead to a null pointer exception, thereby your program becomes safer. Please consider such recommendations while writing your code.

Home
Mobile Site | Full Site
Copyright 2018 © QuinStreet Inc. All Rights Reserved