3

This is my method:

private List<OfficerAccountFilter> createResult(List<Account> accountList) {
        List<List<Account>> partedList = Lists.partition(accountList, MAX_LIMIT);
        List<OfficerAccountFilter> result = new ArrayList<>();
        System.out.println("size of account" + accountList.size());
        int size = accountList.size() / 800;
        ExecutorService executorService = Executors.newFixedThreadPool(NTHREDS);
        List<Future<List<OfficerAccountFilter>>> futureList = new ArrayList<Future<List<OfficerAccountFilter>>>();
        for (int i = 0; i <= size + 1; i++) {
            Callable<List<OfficerAccountFilter>> worker = new AccountCallable(accountList);
            Future<List<OfficerAccountFilter>> submit = executorService.submit(worker);
            futureList.add(submit);

        }
        for (Future<List<OfficerAccountFilter>> future : futureList) {
            try {
                result = future.get();
            } catch (InterruptedException | ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        executorService.shutdown();
        return result;
    }

my callable class:

public class AccountCallable implements Callable<List<OfficerAccountFilter>> {

    @Autowired
    private OfficerAccountSOAPRunner officerAccountSOAPRunner;

    private List<Account> accountList;

    public AccountCallable(List<AccountIdentifier> accountList) {
        this.accountList = accountList;
    }

    @Override
    public List<OfficerAccountFilter> call() throws Exception {

        List<OfficerAccountFilter> result = new ArrayList<>();
        for (Account account : accountList) {
            OfficerAccountRequest request = new OfficerAccountRequest();
            request.getAccounts().add(account);
            List<OfficerAccountFilter> accounts = officerAccountSOAPRunner.getAccounts(request);
            result.addAll(accounts);
        }
        return result;
    }
}

officerAccountSOAPRunner.getAccounts(request); 

this is where im invoking Soap service which gives the filtered accounts back to officer. Not sure why is it returning null at this point. If i do it sequentially, it works absolutely fine. Am i not setting the future Object right?

Error stack:
java.util.concurrent.ExecutionException: java.lang.NullPointerException
    at java.util.concurrent.FutureTask.report(FutureTask.java:122)
    at java.util.concurrent.FutureTask.get(FutureTask.java:192)
    at com.test.service.impl.OfficerServiceImpl.createResult(OfficerServiceImpl.java:92)
    at com.test.ServiceImpl.limitOfficerAccounts(OfficerServiceImpl.java:61)
    at com.test.service.impl.OfficerServiceImpl.getOfficerAccounts(OfficerServiceImpl.java:54)


    Caused by: java.lang.NullPointerException
    at com.test.impl.AccountIdentifierCallable.call(AccountCallable.java:36)
    at com.test.impl.AccountIdentifierCallable.call(AccountCallable.java:1)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)

The error is SOAPRunner call: List<OfficerAccountFilter> accounts = officerAccountSOAPRunner.getAccounts(request);

I put it in debug mode and it's coming as soon as i set on @Autowired. So it's coming from there. Do i need to set the cope differently?

4
  • First, what is the first loop in createResult for? You are always passing the complete accountList into your callables, so you don't need more than one callable created. Second, which line is throwing the NullPointerException?
    – rohitvats
    Aug 29, 2016 at 19:22
  • The purpose of an exception’s stack trace is that it tells you exactly where the problem occurred. Please edit your question and include that stack trace.
    – VGR
    Aug 29, 2016 at 19:29
  • i get java.util.concurrent.ExecutionException: java.lang.NullPointerException on Aug 29, 2016 at 19:32
  • The entire stack trace, please.
    – VGR
    Aug 29, 2016 at 19:34

1 Answer 1

1

The NullPointerException is because you are autowiring your soap service in your callable:

@Autowired
private OfficerAccountSOAPRunner officerAccountSOAPRunner;

You are creating the instances of AccountCallable yourself and not getting them from Spring, which is correct since you need to create the callables yourself. But Spring cannot then help you with autowiring the soap runner. So you should instead pass in OfficerAccountSOAPRunner to your callable as a constructor parameter when your create it's instances in createResult method.

You can get the OfficerAccountSOAPRunner autowired in the class that contains createResult method, so that then it can pass it to the AccountCallable objects when it creates them.

On a side note, you don't need to create multiple callables. The following:

for (int i = 0; i <= size + 1; i++) {
    Callable<List<OfficerAccountFilter>> worker = new AccountCallable(accountList);
    Future<List<OfficerAccountFilter>> submit = executorService.submit(worker);
    futureList.add(submit);
}

can be replaced with:

Callable<List<OfficerAccountFilter>> worker = new AccountCallable(accountList);
Future<List<OfficerAccountFilter>> future = executorService.submit(worker);

Then you can get the results from the single future:

future.get();
8
  • Sorry, im setting the accounts, by mnistake i ommitted that line and thanksfor poitning future task out Aug 29, 2016 at 19:50
  • @zachandcode In this line: request.getAccounts().addAll(list); what is list? Did you mean it to be account?
    – rohitvats
    Aug 29, 2016 at 19:53
  • OK, what does your service implementation return if there is no officer account found? Does it return null or an empty list? Can you post it's code? The issue seems to be in your service implementation.
    – rohitvats
    Aug 29, 2016 at 20:03
  • See that's the thing. When i call that service sequentially without executor interface. it works fine. and if the accounts are null, i throw an IllegalArgumentException and it stops right there. Right now, @Autowired private OfficerAccountSOAPRunner officerAccountSOAPRunner; as null when i put it debug mode. But it works fine when i call it in service class. Since im calling it in Callable class, is there any anything i'm missing? Aug 29, 2016 at 20:10
  • @zachandcode ah yes, I see now.. @Autowired will not make any difference in your callable, because you are creating the instances of AccountCallable yourself. I'll update the answer to reflect this.
    – rohitvats
    Aug 29, 2016 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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