とExecutorService
を同時に実行していCallables
ます。これが私のコードの簡略版です:
ArrayList<Book> objResults = new ArrayList<Book>();
ExecutorService esrExecutor = Executors.newFixedThreadPool(2);
Set<Callable<ArrayList<Book>>> setCallables = new HashSet<Callable<ArrayList<Book>>>();
setCallables.add(new Callable<ArrayList<Book>>() {
public ArrayList<Book> call() throws Exception {
ArrayList<Book> objAmazonResults = new ArrayList<Book>();
try {
Amazon objAmazon = new Amazon();
objAmazonResults = objAmazon.doSearch(strQuery);
} catch (Exception e) {
e.printStackTrace();
}
return objAmazonResults;
}
});
List<Future<ArrayList<Book>>> lstFutures;
try {
lstFutures = esrExecutor.invokeAll(setCallables);
for(Future<ArrayList<Book>> futFuture : lstFutures){
if (futFuture.get().isEmpty() == false) //NullPointerException occurs here
objResults.addAll(futFuture.get());
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
esrExecutor.shutdown();
私はNullPointerException
このビットのコードを取得しますif (futFuture.get().isEmpty() == false)
。このコードのビットが null になる可能性がある理由や方法がわかりません。
私のものを見ると、Callable
すべての例外をトラップし、単にスタック トレースを出力していて、常に を返していることがわかりますnew ArrayList<Book>()
。
SO で他の人が私のコード スープをデバッグするのを手伝うことはあまりありませんが、誰もが障害にぶつかることがありますが、この種の例外は私に 1 つのことを教えてくれました。