0

I have used Maputils.IsEmpty() method.I use apache.commons.collections framework class.

 if (MapUtils.isEmpty(abcdMap)) {
    //steps to execute
    }
return new arraylist<>(abcdmap.values());

gets null pointer exception in return line. When did debug in the eclipse, I could see some field values in variables tab as below.(havnt mentioned all the internal fields) entryset : hashmap$entrySet(id) , keyset:null, values:null,

How can we handle this nullpointerexception ?

Can you suggest some other way of checking null condition for Maps.Thanks in Advance!!

  • you check if it's empty, why not put a "else" to know when it's not? – jhamon Aug 07 '20 at 13:10
  • @jhamon else is not needed for every if, that depends on the requirements. Also, that wouldn't change anything about the NPE, so is not related to the question – Stultuske Aug 07 '20 at 13:11
  • yes @Stultuske Just providing else condition wont help here –  Aug 07 '20 at 13:13
  • @Stultuske OP is checking if it's empty but then creates a new `arraylist` from the map values, even if the map is empty. Not knowing what this `arraylist`class is, that's seems quite related in my opinion... – jhamon Aug 07 '20 at 13:22
  • Something looks wrong in your code `MapUtils.isEmpty(abcdMap)` returns true when map is null. Having said that you can try `return abcdMap == null ? Collections.EMPTY_LIST : new ArrayList<>(map.values());` – Pankaj Aug 07 '20 at 13:26

0 Answers0