Wednesday, April 20, 2016

Careful with the use of & bitwise operator in boolean evaluation

On a particular day when i did not sleep much and was running low on coffee, I made the following stupid error in my code:

if(model != null & model.getVocabulary().countWords() > 0) return;

What i meant to write is:

if(model != null && model.getVocabulary().countWords() > 0) return;

When i put this code in a apache spark job and run in the cluster, it threw Null Pointer Exception. Turns out that the missing "&" is a serious mistake as if model is evaluated to null, "&&" will then skip the right hand side evaluation. However, with "&", both sides are evaluated. As a result of evaluating "model.getVocabulary()" with model being null causing the null pointer exception.

Lesson learnt: should have more sleeping hours instead of wasting time debugging some funny error like above :p


No comments:

Post a Comment