How to avoid null pointer exception while using String?

Sometimes there is a chance that String variable we are using for comparison gives null pointer exception.

i.e. suppose we have variable as str and we are using some constant string constant_str

str.equals( constant_str )      <--  Here is a chance of null pointer exception.

Solution to avoid null pointer :

To avoid this exception we can check like :

  constant_str.equals( str )
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment


Post a Comment