My question is continuation of what I have asked [here][1] [1]: http://salesforce.stackexchange.com/questions/116478/how-to-define-a-map-with-id-and-listcase-of-records How do I replace from: parentChildCase.get(cMap.get(c).ID).add(cMap.get(c)); to this Id (`pId`): Map<id, id> par = new Map<id, id>(); List <case> rcases =[SELECT ... FROM CASE WHERE <other_conditions....>]; for (case c : rcases) { par.put(c.id,c.ParentId); } Id = pId = par.get(cMap.get(c).Parentid); Error `NullPointerException: Attempt to de-Reference a null object` for(ID c : cMap.keySet()) { if(parentChildCase.containsKey(cMap.get(c).ID)) { //parentChildCase.get(cMap.get(c).ID).add(cMap.get(c)); Id pId = par.get(cMap.get(c).Parentid); parentChildCase.get(pId).add(cMap.get(c)); //ERROR NullPointerException: Attempt to de-Reference a null object } else { List<Case> cases = new List<Case>(); cases.add(cMap.get(c)); parentChildCase.put(cMap.get(c).ID, cases); } }