lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ninaS <>
Subject Re: NullPointerException in FieldDocSortedHitQueue.lessThan with custom SortComparator
Date Mon, 02 Feb 2009 13:35:24 GMT

I already found another solution: I don't use a custom SortComparator.
Another solution would be to define a default value for null.

Would be nice if lucene in future would be able 
to search by null values also if a custom SortComparator is used.

To tell you more:

public class MyComparator extends SortComparator {


 public ScoreDocComparator newComparator (final IndexReader reader, final
String fieldname)

throws IOException {
	    final String field = fieldname.intern();
	    final Comparable[] cachedValues 
	    	= FieldCache.DEFAULT.getCustom (reader, field,
	    return new ScoreDocComparator() {

		      public int compare (ScoreDoc i, ScoreDoc j) {
                         ... // compare and handle nulls

	      public Comparable sortValue (ScoreDoc i) {
	        return cachedValues[i.doc];

	      public int sortType(){

	protected Comparable getComparable(String text) {
		return new MyComparable(text); //implements compare(Object a, Object b),
handles nulls


Using such a comparator will cause the exception above mentioned if null
values appear in the sort field.

That is because:

FieldDocSortedHitQueue 163-166:

 case SortField.CUSTOM:{
           c = docA.fields[i].compareTo(docB.fields[i]);

does not check whether docA.fields[i] is null.

As you see in line 134-148:

case SortField.STRING:{
					String s1 = (String) docA.fields[i];
					String s2 = (String) docB.fields[i];
					// null values need to be sorted first, because of how
					// works - in that routine, any documents without a value in the given
field are
					// put first.  If both are null, the next SortField is used
					if (s1 == null) c = (s2==null) ? 0 : -1;
					else if (s2 == null) c = 1;  // 
					else if (fields[i].getLocale() == null) {
						c = s1.compareTo(s2);
					} else {
						c = collators[i].compare (s1, s2);

nulls are handled with the type SortField.STRING.

View this message in context:
Sent from the Lucene - Java Users mailing list archive at

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message