I am working in a multithreaded environment with many methods to be called using Executors. The code works fine even for large data earlier, but it threw a null pointer exception once. Here is the related parts of the code.
private ExecutorService crawlerTaskExecutor = Executors.newFixedThreadPool(CRAWLER_THREAD_POOL_SIZE);
private List<Future<Boolean>> futureList = new ArrayList<>();
futureList.add(crawlerTaskExecutor.submit(() -> fetchDocuments(siteUrl),Boolean.FALSE));
//Many other method calls are also present.
private boolean isCrawlerRunning() {
boolean isRunning = Boolean.FALSE;
if (Objects.nonNull(futureList)) {
for (Future<Boolean> future : futureList) {
if (!future.isDone()) {//got null pointer here.
isRunning = Boolean.TRUE;
break;
}
}
}
return isRunning;
}
I got a null pointer in line if (!future.isDone()). Now I am not getting why would the future be null? I understand that crawlerTaskExecutor.submit is returning null but why and when it can return null? And what could be done if it returns null?
I am not getting why this happened, this issue doesn't get produced earlier even with lots of run with 500k data and suddenly appeared.