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)



2 responses to “Null Pointer Exception when .equals is used

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s