gpt4 book ai didi

java - 迭代流时出现 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 05:18:43 26 4
gpt4 key购买 nike

下面的函数迭代流,但对于某些输入,它会抛出 NullPointerException:

public List<DesignTimeLibrary> getDTLData(List<Project> projects, String projLibLocation, String estimatePath) {
List<DesignTimeLibrary> allDTLs = new ArrayList<DesignTimeLibrary>();
FileProcessor fileProc = new FileProcessor();
XPathProcessor xProc = new XPathProcessor();
projects.stream()
.filter(pJ-> pJ.getDtlNames().size()>0)
.flatMap(project -> project.getDtlNames().stream()).filter(Objects::nonNull)
.forEach(dTL -> {
if(containsName(allDTLs, dTL)) return;
Logger.info("***** Processing DTL "+dTL+" *****");
Map<String, Process> dtlProcesses = null;
List<SharedResource> dtlResources = null;
List<Schema> dtlSchemas=null;
List<Adapter> dtlAdapters = null;
try {
Path pP = fileProc.extractProjLib(projLibLocation + File.separator + dTL, projLibLocation + File.separator + "extracted");
DesignTimeLibrary dtl = new DesignTimeLibrary(dTL, pP);
...
allDTLs.add(dtl);
}
catch (Exception e) {
e.printStackTrace();
}
});
return allDTLs;
}

输出为:

Processing: WCLEClientV002.projlib
Processing: WCLEClientV002.projlib
java.lang.NullPointerException
at com.tbt.data.ProjectProcessor.lambda$getDTLData$1(ProjectProcessor.java:77)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:174)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
at com.tbt.data.ProjectProcessor.getDTLData(ProjectProcessor.java:79)

这里的 .forEach(dTL -> { 是第 79 行。我尝试过调试,但找不到空对象。

最佳答案

NullPointerException 位于第 77 行,即 pJ-> pJ.getDtlNames().size()>0。如果 getDtlNames() 返回 null,则抛出 NullPointerException,因此只需向该过滤器添加 null 检查

 .filter(pJ-> Objects.nonNull(pJ.getDtlNames()) && pJ.getDtlNames().size()>0)

关于java - 迭代流时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59906543/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com