mercredi 22 juillet 2020

Java - IF statement treats value as NULL even though it isn't when checking in debug

This is a strange one so any help would be appreciated.

I'm working on a Spring Batch job that sends transactions to a Kafka topic and when successful the status code is returned as a String: "C". There's a check for if this status code is null and if so it will give an appropriate error message as below to be outputted onto a table later on.

            if (kafkaWriter.getKafkaStatusCode() == null)
            {
                messagingResultCode = CommonConstants.KAFKA_NULL_RESULT;
            }

So what's happening is that for several transactions that are picked up by the job I get a Kafka null result error message because of the above code so I went into debug to see what was happening putting a breakpoint at where messagingResultCode is set.

But when I evaluated "kafkaWriter.getKafkaStatusCode() == null" it showed as "(boolean) false" in Spring Tools Suite. When looking at the KafkaStatusCode it shows as "C" so definitely not null, but it still went into the code anyway.

All getKafkaStatusCode() does is return a String:

public String getKafkaStatusCode() {
    return kafkaStatusCode;
}

This only ever happens on the first job that's been run on the server (I'm using Liberty) and on all subsequent runs the code behaves as expected.

I've tried initialising the String messagingResultCode to null, "" and a default value but the same thing happens every time.

Thanks

Aucun commentaire:

Enregistrer un commentaire