0

like

x = setDetailsFromSegment(getDetails.getProductList().getProductDetails().get(0).getItinerary().getOriginDestinationList(),
                          getDetailsResponse.getProductDetailsList().getProductDetails().get(0).getFareList());

Here setDetailsFromSegment() is my method and I want to check whether the parameters are null or not before invoking the method using java 8.

P.J.Meisch
  • 18,013
  • 6
  • 50
  • 66
  • 1
    has a null check been changed in java-8? – Eugene May 17 '17 at 05:35
  • So, do it. What's the problem? But you really shouldn't have to do that. A method that (at least seems to) return a List shoud never, ever return null. Make them return an empty list. – JB Nizet May 17 '17 at 05:37
  • as JB Nizet mentioned - you can very well check Null before calling this method. And about the list, you need to check for list size as well, else you may encounter ArrayIndexOutOfBoundException. – Abie May 17 '17 at 06:39
  • Also you might want to read about method chaining in regards of the Law of Demeter. – André Stannek May 17 '17 at 07:19
  • @JBNizet ya ,but the getDetails may not always give productList,so i have to verify whether it is empty or not.They wont return null,but when i use get(0) they will encounter ArrayIndexOutOfBound exception – Jerin Kuttiyanickal May 17 '17 at 10:37
  • @Abie yep..you are right! – Jerin Kuttiyanickal May 17 '17 at 10:38

1 Answers1

0

You should avoid the necessarity of null-checks if possible.

If you can change getOriginDestinationList() and getFareList() you should make them return Collections.emptyList(). If you are not returning lists, you might consider to use an java.util.Optional. But be careful where to use and Optional and where not. See for example Should Java 8 getters return optional type?

If you cannot change the return value of the methods, you have to execute the checks before you are calling the methods. Objects.requireNotNull() could be helpful here.

Beside this your code has a problem because of excessive method chaining. You might need to check the return values for every method. And it is hard to read. And I would not use "x" as a variable name ;-).

Community
  • 1
  • 1
Stefan Großmann
  • 866
  • 9
  • 20
  • I am not returning the lists nor i can change the implementation of the lists(ie make them Optional).These lists are got from the object -->'getDetails'.This is a response after I sent the request.So lists in that objects may or may not be null! So while passing as arguments I need to check Null pointer and ArrayIndexOutOfBound. Also the variable name 'x' is just a dummy one.I too won't use the same;) – Jerin Kuttiyanickal May 17 '17 at 10:30
  • Then you have to execute your checks before you call the setDetailsFromSegment. I would move the two calls into a seperate method where you can apply all necessary checks. Your call could look like '... = setDetailsFromSegment(getOriginDestinationList(), getFareList());'. The methods 'getOriginDestinationList()' and 'getFareList()' could do all necessary checks (if-statements) and they should return an empty list if no result is present. – Stefan Großmann May 17 '17 at 13:31