Streams: Avoid NullPointerException
<p>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)</p>

<p>Here is my code now:</p>

<pre><code>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();
}
</code></pre>

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

<p><strong>UPDATE</strong></p>

<p>Sorry for the lack of precision:
I also sort on the date.</p>

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

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

        cos.endText();
    }
</code></pre>

<p>I get a NullPointerException:/
Thank you for you help</p>