public int compare(Object o1, Object o2) { final Document d1 = (Document) o1; final Document d2 = (Document) o2; try { return compareDocuments(d1, d2); } catch (NullPointerException npe) { NullPointerException nullPointerException = new NullPointerException( "Tried sorting on null fields! You need to call .nullsFirst() or .nullsLast() on your Comparator."); nullPointerException.initCause(npe); throw nullPointerException; } }