lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Praveen Peddi" <>
Subject Re: sorting on a field that can have null values
Date Wed, 29 Dec 2004 18:53:25 GMT
Sorry for the late response. I didn't cheak the reply till now.

I think sorting on a field that doesn't exist for every doc is throwing NullPointerException
for me (if its of type string). FYI: I am using my own comparator for string (see below for
the code). I am sure something is wrong in my comparator but having hard time figure out whats
wrong. I get NullPointerException all the time if some of the fields are null. When I use
default lucene's comparator for string, its fine. May be someone can put their eyes on the
code below and figure out that problem is very obvious. I found that some of the comparables
are null so NullPointerEception is occuring in at line 36.


public class StringIgnoreCaseSortComparator extends SortComparator {
 private static StringIgnoreCaseSortComparator stringIgnoreCaseSortComparator;
 private StringIgnoreCaseSortComparator() {
  protected Comparable getComparable(String termtext) {  
  return new StringIgnoreCaseComparable(termtext);
 public static synchronized StringIgnoreCaseSortComparator getInstance() {
  if(stringIgnoreCaseSortComparator ==null) {
   stringIgnoreCaseSortComparator = new StringIgnoreCaseSortComparator();
  return stringIgnoreCaseSortComparator;
 class StringIgnoreCaseComparable implements Comparable {
  private String stringToCompare;
  public StringIgnoreCaseComparable(String stringToCompare) {
   this.stringToCompare = stringToCompare;

  public int compareTo(Object o) {   
   int result = 0;
   if(stringToCompare==null) {//put it at the bottom (compatible with db sorting)
    result = 1;
   } else if(o==null || ((StringIgnoreCaseComparable)o).getStringToCompare() == null) {
    result = -1;
   } else {
   return result;
 public boolean equals(Object obj) {
   if(stringToCompare==null || obj==null) {
    return false;//if either of the objects are null, return false
   return stringToCompare.equalsIgnoreCase(((StringIgnoreCaseComparable)obj).getStringToCompare());
  public String getStringToCompare() {
   return stringToCompare;


----- Original Message ----- 
From: "Chris Hostetter" <>
To: "Lucene Users List" <>
Sent: Thursday, December 23, 2004 4:43 AM
Subject: Re: sorting on a field that can have null values

> : I thought of putting empty strings instead of null values but I think
> : empty strings are put first in the list while sorting which is the
> : reverse of what anyone would want.
> instead of adding a field with a null value, or value of an epty string,
> why not just leave the field out for that/those doc(s)?
> there's no requirement that every doc in your index has to have the exact
> same set of fields.
> If i rememebr correctly (you'll have to test this) sorting on a field
> which doesn't exist for every doc does what you would want (docs with
> values are listed before docs without)
> -Hoss
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message