简体   繁体   中英

Lucene: How to add Hits to ArrayList

I'm trying add Hits to List() (to populate the results on web pages) this way:

List<ScoreDoc> results = null;

if (hits.scoreDocs.length != 0) {
  for(ScoreDoc scoreDoc : hits.scoreDocs) { 
    results.add(scoreDoc);
  }
}

and it reports:

Exception in thread "main" java.lang.NullPointerException
at _test.Searcher.search(Searcher.java:68)
at _test.Searcher.main(Searcher.java:80)

for the results.add(scoreDoc); line.

What I'm doing wrong?

results is null . You will have to create a List before adding items (for example use an ArrayList ).

List<ScoreDoc> results = new ArrayList<ScoreDoc>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM