NullPointerException thrown from out of nowhere

Today I spent 30 minutes debugging a code which was throwing a null pointer exception. I never seen it before so I ruled out the obvious suspects and checked during several debugging runs what is and what actually is not null. It was not my code and it looked a bit shady, but everything seemed fine in debugger and more importantly, nothing seemed to be null.

Finally after banging my head a bit I traced it down to damned unboxing null pointer.

Consider following code:

class Test {
	public Integer getRowType() {
		return null;
	}
	public void handleRowType(int rowType) {
		// not relevant
	}

	public void main(String[] args) {
		handleRowType(getRowType()); // NPE is thrown on this line.
	}
}

If you look at it like this then the error is  obvious to the experienced eye. getRowType returns null Integer. handleRowType method requires primitive int type so Java auto-unboxes the supplied Integer and throws the NPE because the Integer returned is null. Hopefully this will save someone few bangs to the head :).