5

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..
}
3
  • Please don't use HTML tags in your questions.
    – assylias
    Jan 30, 2013 at 10:02
  • what are the values returned by the callable? Jan 30, 2013 at 10:03
  • Did you figure out what was going on?? Oct 3, 2013 at 19:35

2 Answers 2

3

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.

1
  • Sure, I'l check that. Thanks for ur reply! :) Jan 31, 2013 at 4:18
0

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

    for ( Future< Boolean > result : resultList ) {
    result.get(); // getting exception here..
}

resultsList is not an array in your code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.