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..
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With