Frakcool
Frakcool

Reputation: 11153

Check if a String is null or empty when it's null sometimes and sometimes not

Well I dont usually write this kind of questions, but even after checking:

Java, check whether a string is not null and not empty? and Checking if a string is empty or null in Java.

My program creates a PDF with the information that has changed in the original contract and the modified one, but only these information. If the information is the same in both contracts it's not painted. But if it's not, then it paints it.

I have no problem when there's something in both contracts, for example notes.

If on the original contract they don't write anything but they do on the modification (Endorsement), then the system should not paint anything of the notes field. Same if they wrote something and delete it on modification. Here's where the problem lies.

When I try to compare:

if ((sSPA.getNotes() == null ||
     sSPA.getNotes().length() == 0) &&
    (sSPE.getNotes() != null ||
     sSPE.getNotes().length() != 0)) {
    //Some stuff here
}

I've tried too

if ((sSPA.getNotes() == null ||
     sSPA.getNotes().equals("")) &&
    (sSPE.getNotes() != null ||
     !sSPE.getNotes().equals(""))) {
    //Some stuff here
}

And

if ((sSPA.getNotes() == null ||
     sSPA.getNotes().isEmpty()) &&
    (sSPE.getNotes() != null ||
     !sSPE.getNotes().isEmpty())) {
    //Some stuff here
}

I get the following error in log in execute time:

java.lang.NullPointerException
at com.dnhr.rwa.server.document.PdfEndorsement.renderSubscriptionSubmissionProp(PdfEndorsement.java:1545)

And I know it's because the field is null and I'm trying to get it's length, or compare it to an empty string, and it's trying to do:

if (null.equals("")) { and that throws the exception same with length() method.

And if I try doing:

if ((sSPA.getNotes() == null ||
     getStringValue(sSPA.getNotes().equals(""))) &&
    (sSPE.getNotes() != null ||
     !getStringValue(sSPE.getNotes().equals("")))) {
    //Some stuff here
}

It returns the following error in compile time:

[javac] /home/efrain/proyectos/rwa-test/trunk/src/com/dnhr/rwa/server/document/PdfEndorsement.java:1541: error: bad operand types for binary operator '||'
[javac]     if ((sSPA.getNotes() == null ||
[javac]                                       ^
[javac]   first type:  boolean
[javac]   second type: String

Is there any other way to compare if the field is null or empty

Here's an example of how I compare the 3 possibilities:

if((sSPA.getNotes() == null || sSPA.getNotes().trim().equals("")) &&
    (sSPE.getNotes() != null || !sSPE.getNotes().equals(""))) {
    //some stuff
} else if(!getStringValue(sSPA.getNotes()).
      equals(getStringValue(sSPE.getNotes())) &&
      (sSPE.getNotes() == null || 
       sSPE.getNotes().trim().equals(""))) {
        //some stuff
} else if(!getStringValue(sSPA.getNotes()).
      equals(getStringValue(sSPE.getNotes()))) {
        //some stuff
}

Upvotes: 0

Views: 1031

Answers (3)

tomse
tomse

Reputation: 501

Is there any other way to compare if the field is null or empty

You could simply use a helper method like this:

static boolean nullOrEmpty(Stirng s) {
  return s == null || s.isEmpty();
}

which would make your code more readable.

Upvotes: 4

laubed
laubed

Reputation: 248

Just write

if( sSPA.getNotes() == null ||
    (sSPA.getNotes() != null && sSPA.getNotes().isEmpty()) ... 

Case 1: sSPA.getNotes() is null the first expression evaluates to true. Therefore if case is being executed

Case 2: sSPA.getNotes() is not null but empty the first expression is false thereforce the second is evaluated. Because sSPA is not null the first subexpression passes and sSPA.getNotes.isEmpty() returns true. the overall expression is true -> the if case gets executed.

Case 2: sSPA.getNotes() is not null and not empty Every expression evaluates to false except the != null case. In overall the complete condition is false -> the if case gets not executed.

Upvotes: 1

Soham
Soham

Reputation: 4417

I think the best option to check null pointer is

if(sSPA.getNotes()!=null && sSPA.getNotes().trim().length()>0){

}else{

//null

}

Upvotes: 0

Related Questions