Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExecutorService.Submit(Callable<T>) is returning null value for Future<T> object

I'm using ExecutorService to spawn threads for performing different tasks. When the submit(Callable<T>) method is made, it should return a Future<T> object. Instead, it is returning null. As a result, when Future<T>.get() method is called, it will fail with NullPointerException.

Has any one faced this problem? Or, am I doing something wrong?

ArrayList<Boolean> resultsList = new ArrayList<Boolean> ();
ExecutorService excutorService  = Executors.newCachedThreadPool();
for(ClassImplementingCallable myClassImplementingCallable : listOfClassesImplementingCallable) {
    resultsList.add( excutorService.submit( myClassImplementingCallable ) );
}
excutorService.shutdown();
for ( Future< Boolean > result : resultList ) {
    result.get(); // getting exception here..
}
like image 413
user2024707 Avatar asked Oct 22 '22 18:10

user2024707


1 Answers

What is surprising is that you could compile your code: you are trying to add some Future<Boolean> to a List<Boolean>...

resultsList should be declared as:

List<Future<Boolean>> resultsList = new ArrayList<Future<Boolean>> ();

Apart from that, you should not receive a NPE with your code as it is, unless one of the myClassImplementingCallable in listOfClassesImplementingCallable is null.

like image 96
assylias Avatar answered Oct 31 '22 00:10

assylias