0

I am having the below given data structure.
In the given code block, Im trying to get Object Dc from the given HashMap dealCommits.
The piece of code will work if the map has got the object associated with the given key (cNumber) is available.
My problem is how to handle the null pointer in line (//5) when the given key is not found in the map.
It will be a great help if somebody can throw some light to amend this stream based code to handle the exception.

public class Dc {   
private String cNumber;     
private Optional<List<pTerms>> pTerms;   
}    

public class Dd {   
private String cParty;   
//Dc:cNumber is the Key   
private Optional<Map<String,Dc>> dealCommits;   
}    

public class PTerms {   
private String pType;  
}     


public String check(String tMonth,String cNumber,Dd dDetails)      
{     
Optional<DealPricingTerms> dealPricingTerms = dDetails    
    .getDealCommits().get()     
    .get(cNumber)     
   .getPTerms().get().stream()   //5      
    .filter(dealPricingTerm ->  
            tMonth.equals(dealPricingTerm.getDeliveryPeriod()))      
    .findFirst();        
    return dealPricingTerms.isPresent()? "Success" : "failed";  
}   
1
  • 2
    Wondering: why bother with Optional, when you then unconditionally assume that the Optional contains a value? That is like walking out in the rain with an umbrella, but then being too lazy to open and use the umbrella?! Commented Jun 9, 2019 at 8:15

2 Answers 2

1

You shouldn't call get() on your Optionals - and when you use Optional.map you have a nice way to wrap a null result from a Map get into another Optional:

Optional<PTerms> dealPricingTerms = dDetails
        .getDealCommits().map(c -> c.get(cNumber))
        .flatMap(dc -> dc.getPTerms())
        .map(l -> l.stream())
        .flatMap(s ->
                s.filter(dealPricingTerm ->
                        tMonth.equals(dealPricingTerm.getDeliveryPeriod()))
                        .findFirst());
Sign up to request clarification or add additional context in comments.

1 Comment

@Erwin Bolwidt, Thanks a lot and hats off to you :) . You have transformed my code to a beautiful one :)
0

You have to add filter to check null before your condition filter.

.filter(Objects::nonNull)

for example,

 List<String> carsFiltered = Optional.ofNullable(cars)
            .orElseGet(Collections::emptyList)
            .stream()
            .filter(Objects::nonNull) //filtering car object that are null
            .map(Car::getName) //now it's a stream of Strings
            .filter(Objects::nonNull) //filtering null in Strings
            .filter(name -> name.startsWith("M"))
            .collect(Collectors.toList()); //back to List of Strings

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.