Lucene TF-IDF分数计算器中的NullPointerException

时间:2016-10-25 12:50:06

标签: java nullpointerexception lucene tf-idf

我在Lucene 6.1.0中制作了TF-IDF得分计算器。即使传递字段名称和术语名称,我的分数计算器也会显示空指针异常。下面是在主类中出现错误的代码部分。

public static void main(String[] args) throws IOException {

Tf_Idf tfidf = new Tf_Idf();


String field = "contentfield";
        String term = "Reuters";

tfidf.scoreCalculator(field, term);    //Line 144

  }

现在scoreCalculator功能如下:

 public void scoreCalculator (String field, String term) throws IOException 
{


    TFIDFSimilarity  tfidfSIM = new  ClassicSimilarity();

 // Bits liveDocs = MultiFields.getLiveDocs(this.indexReader);
//line 247        TermsEnum termEnum = MultiFields.getTerms(this.indexReader, field).iterator();    
    BytesRef bytesRef=null;
    while ((bytesRef = termEnum.next()) != null) {
        if(bytesRef.utf8ToString().trim().equals(term.trim())) {
            if(termEnum.seekExact(bytesRef)) {
            int doc;
                idf = tfidfSIM.idf(termEnum.docFreq(),     this.indexReader.numDocs());
                PostingsEnum docsEnum = termEnum.postings(null);
                if(docsEnum != null) {
                     doc=0;}
                    while((doc = docsEnum.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
                        tf = tfidfSIM.tf(docsEnum.freq());
                        tfidf_score = tf * idf ;
                        System.out.println(" -tfidf_score-" + tfidf_score);
                    }

            }
        }

    }

   }

}

错误信息

     Exception in thread "main" java.lang.NullPointerException
     at Lucene.Tf_Idf.scoreCalculator(IndexFiles.java:247)
     at Lucene.IndexFiles.main(IndexFiles.java:144)

索引已完成,但得分计算器无法正常工作。此外,我认为将值传递给term和field是错误的。请帮我弄清楚如果不是这样的话。

编辑:

是的,我在tf-idf构造函数中打开了索引阅读器。

class Tf_Idf {
static float tf = 1;
static float idf = 0;
private float tfidf_score;
static float [] tfidf = null;


 IndexReader indexReader;


    public Tf_Idf() throws IOException {
         String indexPath = "/home/kriti/index4";
    this.indexReader = DirectoryReader.open(FSDirectory.open(Paths.get(indexPath)));

}

我也发现改变之后          //String Field="contentsfield"           String Field="contents"; 现在没有显示错误,但仍然没有计算tf-idf值。我传递错误的字符串或其他东西吗?除了这些是我得到的一些警告信息

Null point access:Variable docsEnum can only be null at this location 
The value of local variable doc is not used

这些是我给出的字段的名称:

 static void indexDoc(IndexWriter writer, Path file, long lastModified) throws IOException {
try (InputStream stream = Files.newInputStream(file)) {
  // make a new, empty document
  Document doc = new Document();


  Field pathField = new StringField("path", file.toString(), Field.Store.YES);
  doc.add(pathField);


  Field modifiedfield=new LongPoint("modified", lastModified);
  doc.add(modifiedfield);


  Field contentfield=new TextField("contents", new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)));
  doc.add(contentfield);

  if (writer.getConfig().getOpenMode() == OpenMode.CREATE) {

    System.out.println("adding " + file);
    writer.addDocument(doc);
  } else {
     System.out.println("updating " + file);
    writer.updateDocument(new Term("path", file.toString()), doc);
    }
  }
}
 }

1 个答案:

答案 0 :(得分:0)

看起来this.indexReader为空。在调用scoreCalculator方法之前,请确保已打开阅读器(即在Tf_Idf构造函数中)。