2016-10-25 4 views
0

Lucene 6.1.0でTF-IDFスコア計算ツールを作成しました。フィールド名と用語名を渡しても、私のスコア計算機はヌルポインタ例外を表示しています。以下はエラーのあるメインクラスのコードの一部です。LuceneのNullPointerException 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) 

インデックスが行われたが、スコア計算は私がで間違っていると思うworking.Alsoではないです値をtermとfieldに渡してください。もしそうでなければ何を渡すべきかを理解するのを助けてください。

EDIT:

はい、私は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 values.Amを計算されていないことはありますか?これらは、これらの名前である

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); 
    } 
    } 
} 
} 

答えて

0

のように見えます。 scoreCalculatorメソッドを呼び出す前に、リーダー(つまり、Tf_Idfコンストラクタ)を開いていることを確認してください。

+0

私は自分の答えを編集しました。インデックスリーダーは決して空ではありませんでした。他に何が問題になるか教えていただけますか? –

+0

@KritiDutta - 特に、実際には存在しないフィールド名を与える場合、 'getTerms'がnullを返すことがあります。通常、このようなNPEはデバッグするのが難しくありません。選択したIDEでデバッグする方法を知っていますか? – femtoRgon

関連する問題