猿问

使用 lambda 时处理空指针

nullPointerException除非e.getKey()返回,否则下面的代码会得到吗null


Map<Integer, Optional<SecurityAttributeChange>> spnCreationDetails;

Optional.ofNullable(spnCreationDetails.get(e.getKey()).orElse(new SecurityAttributeChange()).getNewAttribute())

                                    .orElse(new SecurityAttr())

                                    .getUserName());

我在这一行中遇到了 NPE,是否可以调试它?我知道spnCreationDetails.get(e.getKey())返回 null,我null在这里处理时遗漏了什么吗?


- 编辑


这是几乎完整的方法,它将有助于提供有关如何重构它以使其可读的输入。


private void updateAuditFields(List<SecurityAttributeChange> securityChanges, Map<Integer, Map<String, Object>> result) {


    Map<Integer, Optional<SecurityAttributeChange>> spnModificationDetails = ...


    Map<Integer, Optional<SecurityAttributeChange>> spnCreationDetails = ...


    result.entrySet().stream().forEach(e -> {

        e.getValue()

                .put("userIdLastChanged",

                        Optional.ofNullable(Optional.ofNullable(spnModificationDetails.get(e.getKey()))

                                .orElse(Optional.of(new SecurityAttributeChange()))

                                .get()

                                .getNewAttribute()).orElse(new SecurityAttr()).getUserName());


        e.getValue()

                .put("lastChangedDatetime",

                        Optional.ofNullable(Optional.ofNullable(spnModificationDetails.get(e.getKey()))

                                .orElse(Optional.of(new SecurityAttributeChange()))

                                .get()

                                .getNewAttribute()).orElse(new SecurityAttr()).getKnowledgeBeginDate());


        e.getValue()

                .put("userIdCreated", Optional.ofNullable(

                        Optional.ofNullable(spnCreationDetails.get(e.getKey())).orElse(Optional.of(new SecurityAttributeChange())).get().getNewAttribute())

                        .orElse(new SecurityAttr())

                        .getUserName());



    });

}


慕桂英546537
浏览 260回答 1
1回答

泛舟湖上清波郎朗

如果spnCreationDetails.get(e.getKey())可以为空,您可能需要:Optional.ofNullable(spnCreationDetails.get(e.getKey())) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.orElse(new&nbsp;SecurityAttributeChange()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.getNewAttribute() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.orElse(new&nbsp;SecurityAttr()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.getUserName();iespnCreationDetails.get(e.getKey())应该用Optional.编辑:看到您的评论,spnCreationDetails.get(e.getKey())可以返回 anull或 an&nbsp;Optional<SecurityAttributeChange>,因此最好只将 thenull转换为 new&nbsp;Optional:spnCreationDetails.getOrDefault(e.getKey(),Optional.empty()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.orElse(new&nbsp;SecurityAttributeChange()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.getNewAttribute() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.orElse(new&nbsp;SecurityAttr()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.getUserName();
随时随地看视频慕课网APP

相关分类

Java
我要回答