Null Pointer Exception when .equals is used

Consider an example:

i.          a.equals(b)

The value of “a” should not be evaluated as NULL.

ii.       pwd1.equals(pwd2)

pwd1- password from database

pwd2- password entered by user

Returns NULL if “pwd1” is not found in database.

When I tried to use this code line, it gave Null pointer exception. The value of “pwd2” cannot be NULL as it is validated for not null before this code. When “pwd1” is searched in database to check if it matches “pwd2” and no value exists, it returns NULL.

So, change it to pwd2.equals(pwd1)

iii.          Str.equals(‘match’)

” match ” is a constant so it cannot be Null. But “Str” if doesnt contain any value can give a Null value. So, better change to    ‘match’.equals(Str)

 

Advertisements