목록에 null 값이 있으므로 null 포인터 예외가 있습니다. adPics
. 거의 발생하지 않습니다. 그게 어떻게 가능해? 정렬 중에 매우 이상한 NullPointerException
List<String> downloadAdImages(List<String> imagesUrls, final String itemFolder) {
final List adPics = new ArrayList<>();
final ExecutorService executor = newFixedThreadPool(20);
imagesUrls.forEach(
picUrl -> executor.submit(() -> {
try {
String imageNewFileName = imagesUrls.indexOf(picUrl) + "." + getExtension(picUrl);
String bigPicUrl = picUrl.replace("b.jpg", "ab.jpg"); // big version
copyURLToFile(new URL(bigPicUrl), new File(itemFolder, imageNewFileName), 10, 10);
adPics.add(imageNewFileName);
} catch (IOException ex) {
log.log(Level.WARNING, "Could not download image {0} ({1})", new Object[]{picUrl, ex.getMessage()});
}
}));
executor.shutdown();
try {
executor.awaitTermination(15L, MILLISECONDS);
} catch (InterruptedException ex) {
log.log(Level.WARNING, "Could not wait for all images downloads");
}
Collections.sort(adPics); // null values at list lead to NPE here. How are there null values?
return adPics;
}
때때로
adPics
목록
null
값이 있습니다. 그것이 NPE의 이유입니다. 그러나 어떻게? 스레드에서 실행 된 코드를 분석하면
null
값을 추가 할 수 없습니다. 이미지를 다운로드 할 때 문제가 발생하면 IOException이 발생합니다.
imageNewFileName
은
null
일 수 없습니다.
이 코드는 Java 8이며 Apache Commons IO lib를 사용합니다.
(약 질문, downvotes) 나는 당신이 여러 스레드에서 목록에 추가하기 때문에 목록이 제대로 동기화되지 않은 것 같아요. 웃기는 일은 그렇게 할 때 일어날 수 있습니다. – sstan
'awaitTermination' 호출이 시간 초과되지 않았으며,'adPics'를 정렬하면서 동시에 요소를 추가하고 있습니까? 왜 그것이 NPE를 던질 지 모르겠지만'ArrayList'는 스레드로부터 안전하지 않으므로 아무 일도 일어나지 않을 것입니다. – Tunaki
흠 .. thanks @sstan http://stackoverflow.com/questions/11360401/java-synchronized-list –