为了索引我的推文集,我编写了这段代码,但出现一个错误,我不知道是什么,我应该初始化几件事情,谢谢。索引推文时出错
public static void main (String args []) throws IOException{
String line;
Gson gson = new GsonBuilder().create();
File f = new File(Constante.DATA);
BufferedReader reader;
Indexer indexer = new Indexer(Constante.INDEX);
int nbrtweet =0, i=0;
for(File file : f.listFiles())
{
System.out.println(file.getName());
reader = new BufferedReader(new FileReader(file));
while((line = reader.readLine())!=null){
Tweet tweet;
try{
tweet = gson.fromJson(line, Tweet.class);
}catch(JsonSyntaxException e){
i++;
continue;
}
nbrtweet++;
indexer.indexTweet(tweet);
}
错误显示了在这条线:
for(File file : f.listFiles())
,并打印
Exception in thread "main" java.lang.NullPointerException
at ApprocheTwitter.src.Indexer.IndexationThematiqueTwits.main(IndexationThematiqueTwits.java:26)
对不起,但我不明白以前的解决方案 – celia
@JonSkeet我不同意这是一个“简单”NPE错误的副本;如果你知道Java'File' API的缺点,你知道我的意思 – fge
我不知道这就是为什么我问这个问题。谢谢 – celia