java 流:避免 NullPointerException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39617625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:31:22  来源:igfitidea点击:

Streams: Avoid NullPointerException

javajava-8

提问by Adam D

I have to get the first element of an array., but it is possible that the element is empty; If the element is empty I put an empty field (I am trying to generate a pdf)

我必须获取数组的第一个元素。但该元素可能为空;如果元素为空,我将放置一个空字段(我正在尝试生成一个 pdf)

Here is my code now:

这是我现在的代码:

public void makePdf(Long id) throws IOException {
    Candidacy ca = candidacyRepository.findOne(id); 
    cos.beginText();

    cos.showText(
       ca.getInterviews().stream().map(Interview::getAgency).map(Agency::getAgencyName).collect( Collectors.toList()).get(0)!=null?ca.getInterviews().stream().map(Interview::getAgency).map(Agency::getAgencyName).collect( Collectors.toList()).get(0):"")); 

    cos.endText();
}

So I will wish not to prevent the generation of the pdf. Thank you very much for your support!

所以我不想阻止pdf的生成。非常感谢您的支持!

UPDATE

更新

Sorry for the lack of precision: I also sort on the date.

抱歉不够精确:我也按日期排序。

public void makePdf(Long id) throws IOException {
        Candidacy ca = candidacyRepository.findOne(id); 
        cos.beginText();

        cos.showText(
           ca.getInterviews().stream().sorted((a,b)-> a.getInterviewDate().compareTo(b.getInterviewDate())).sorted((a,f)->f.getInterviewDate().compareTo(a.getInterviewDate())).sorted((b,f)->b.getInterviewDate().compareTo(f.getInterviewDate())).map(Interview::getAgency).map(Agency::getAgencyName).collect( Collectors.toList()).get(0)!=null?ca.getInterviews().stream().sorted((a,b)-> a.getInterviewDate().compareTo(b.getInterviewDate())).sorted((a,f)->f.getInterviewDate().compareTo(a.getInterviewDate())).sorted((b,f)->b.getInterviewDate().compareTo(f.getInterviewDate())).map(Interview::getAgency).map(Agency::getAgencyName).collect( Collectors.toList()).get(0):"")); 

        cos.endText();
    }

I get a NullPointerException:/ Thank you for you help

我收到一个 NullPointerException:/ 谢谢你的帮助

回答by Eran

This code doesn't make sense. You are executing the same Streampipeline twice, and each time you generate an entire Listwhen you only need the first element of that List.

这段代码没有意义。您正在执行相同的Stream管道两次,每次生成一个完整的管道List时,您只需要该管道的第一个元素List

You can use findFirstto get the first element of the Stream.

您可以使用findFirst来获取Stream.

EDIT :

编辑 :

After testing my original answer, it turned out it doesn't work. findFirst()throws a NullPointerExceptionif the first element is null.

在测试了我的原始答案后,结果证明它不起作用。如果第一个元素是 ,则findFirst()抛出 a 。NullPointerExceptionnull

You can avoid that by setting the default value before calling findFirst():

您可以通过在调用之前设置默认值来避免这种情况findFirst()

ca.getInterviews().stream()
                  .map(Interview::getAgency)
                  .map(Agency::getAgencyName)
                  .map(s->s!=null?s:"") // this will replace null with ""
                  .firstFirst()
                  .get();