具有多个callables的空指针异常

时间:2018-05-26 17:09:04

标签: java multithreading nullpointerexception thread-safety

我收到以下代码的Null Pointer Exception。

private class ProcessingThread implements Callable<List<Object>> {

        @Autowired
        private DataRetrievalService dataRetrievalService;
        String startDateTime;
        String endDateTime;
        int stationId;
        Set<String> channels;
        List<Integer> sensors;

        public ProcessingThread(String startDateTime, String endDateTime, int stationId, Set<String> channels,
                List<Integer> sensors) {
            super();
            this.startDateTime = startDateTime;
            this.endDateTime = endDateTime;
            this.stationId = stationId;
            this.channels = channels;
            this.sensors = sensors;
        }

        @Override
        public List<Object> call() throws Exception {
            System.out.println("I was called!!");
            return dataRetrievalService.getTimeSeriesData(stationId, channels, sensors, startDateTime, endDateTime);
        }
    }

来电方式: -

int noOfYears=1;
for (int i = 0; i < noOfYears; i++) {
            if (i == 0)
                endDateTime = dateTimeUtil.getOneYearLaterDate(startDateTime);
            else {
                startDateTime = dateTimeUtil.getOneYearLaterDate(startDateTime);
                endDateTime = dateTimeUtil.getOneYearLaterDate(endDateTime);
            }
            Callable<List<Object>> callable = new ProcessingThread(startDateTime, endDateTime, stationId, channels,
                    sensors);
            futures.add(executorService.submit(callable));
        }
        executorService.awaitTermination(300, TimeUnit.SECONDS);
        for (Future<List<Object>> future : futures) {
            int i = 0;
            if (i == 0)
                b = new ArrayList<>(future.get());
            else
                b.add(future.get());
            i++;
        }
}

调用它总是在300秒后给出空指针异常,未来本身变为空。虽然如果我正常执行代码工作正常!任何人都可以解释一下我在哪里做错了!

调用以下代码一切正常!

for (int i = 0; i < noOfYears; i++) {
            if (i == 0)
                endDateTime = dateTimeUtil.getOneYearLaterDate(startDateTime);
            else {
                startDateTime = dateTimeUtil.getOneYearLaterDate(startDateTime);
                endDateTime = dateTimeUtil.getOneYearLaterDate(endDateTime);
            }
            futures.add(executorService.submit(new Callable<List<Object>>() {

                @Override
                public List<Object> call() throws Exception {
                    return dataRetrievalService.getTimeSeriesData(stationId, channels, sensors, "2010-04-11 12:23:00.0", "2011-04-11 12:23:00.0");
                }
            }));

        }
        executorService.awaitTermination(300, TimeUnit.SECONDS);
        int i = 0;
        for (Future<List<Object>> future : futures) {
            if (i == 0)
                b = new ArrayList<>(future.get());
            else
                b.add(future.get());
            i++;
        }

因为这里startDateTime和endDateTime必须被声明为final或者有效,所以我不能使用这个代码,这个2对我来说是变量,并且对于for循环中的每次迭代都会改变。

1 个答案:

答案 0 :(得分:0)

int i = 0;移至循环外部。

您继续使用value = 0重新创建它,b arraylist在循环后只包含一个最终值。

您还有依赖问题,您应该将ProcessingThread标记为bean(将其公开并使用@Component@Service

进行注释
@Component
public class ProcessingThread implements...

没有它,您的DataRetrievalService dataRetrievalServicenull,因此会抛出NPE。