1

I am using following code to check if the String is empty

if( !Index2.toString().isEmpty()){
  ....
}

But I run into following error

java.lang.NullPointerException
2
  • Index2.toString().isEmpty() should be Index2!=null && Index2.toString().isEmpty() Commented Oct 12, 2012 at 6:40
  • Hello, Index2 is it your own class? Probably you redefined toString() method, and it returns null now. Which in turn causes NPE. I do not think it's a good idea to have blahblahblah.toString().isEmpty() code, could you please highlight the root of the problem, it could probably be the issue with the method you've choosen, as for example, you are trying to check if index object is filled - it would not be a good idea to check string, it would be rather better to have utility method or instance method which checks. Commented Oct 12, 2012 at 7:33

6 Answers 6

4

Your Index2 variable has null value. That's why you get the error. Specifically, the error is here:

Index2.toString()

Assuming your Index2 variable is different from null, your code will work. In order to make your code to work, you can validate that your Index2 is different from null or add a try/catch block that handles NullPointerException.

First way:

if( Index2 != null && !Index2.toString().isEmpty()){
    //...
}

Second way:

try {
    if( !Index2.toString().isEmpty()){
      //...
    }
} catch (NullPointerException npe) {
    //handle the Exception...
}

IMHO I would use the first way.

Sign up to request clarification or add additional context in comments.

1 Comment

@EmertanaEm Use Exception handling only on exceptional cases, it's time consuming and shouldn't be used in the normal flow of the application
2

Check Null also.

if( Index2!=null && !Index2.toString().isEmpty()){
  ....
}

Comments

2

There's a StringUtils class in the apache.commons package that has lots of useful methods, like

StringUtils.isEmpty(theString)

Which will do both comparisons, != null and isEmpty and looks better.

Comments

2

Check for String not to be null as well check for the empty space in a String because even if we have a space in a String as myString=" "; myString.isEmpty() will return false . so please considering the case above i will suggest as :

  if (myString != null && myString.trim().length() != 0 ) 
               return true;

Comments

0

This is how you should check empty for your case:

 if( Index2 == null || Index2.toString() == null || 
     Index2.toString().length() == 0)
     // it is empty

Comments

0

isEmpty() and null has nothing to do with each other. First checks the value of String if its equal to "" - OR you can say it will return true if its length is only and only 0. And second is checking for its initialization.

You should check for both of these condition if needed, like this,

if( str != null && !str.isEmpty() ) { ... }

OR

if( str != null && !str.equals("") ) { ... }

OR use this in your case,

if(index2 != null) { if( index2.toString() != null && !index2.toString().isEmpty() ) { ... } }

2 Comments

if you are creating your String from any other object then you can use if(index2 != null) { if( index2.toString() != null && !index2.toString().isEmpty() ) { ... } }
@Emertana Em I've did exactly the same thing in comment. But I gave some explanation too. That's why did you undo Accept ... ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.