0

My question is continuation of what I have asked here

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);
    }
}

1 Answer 1

1

The reason why you have a null reference is because of this

if(parentChildCase.containsKey(cMap.get(c).ID))   // **you are checking for the key 
    {
        //parentChildCase.get(cMap.get(c).ID).add(cMap.get(c));
        Id pId = par.get(cMap.get(c).Parentid);
       // **you are inserting on a different key here
        parentChildCase.get(pId).add(cMap.get(c)); //ERROR      NullPointerException: Attempt to de-Reference a null object
    }

If my understanding is correct you wanna create a map of Map<pID, List<Case>>

All you need to loop in your Map keys and create a new Map

Map<Id, List<Case>> PIDMap = new Map<Id, List<Case>>();
for(ID c : cMap.keySet()) 
{
    if(parentChildCase.containsKey(cMap.get(c).ID)) 
    {
      Id pId = par.get(cMap.get(c).Parentid); 
      PIDMap.put(pID,parentChildCase.get(cMap.get(c).ID) );
    }
}
3
  • the way i wanted to have output is pareidId: {child case},{child case}, like one to many relationship so I should be able to pull one parent id has 1 or more child cases. if you look at my question I have the link and it will take you to my original question.. as i said its continuation of my question, in this question, I just wanted to change to correct ParentId
    – Nick
    Commented Apr 4, 2016 at 13:56
  • I am not 100% sure what you mean exactly...
    – raym0nd
    Commented Apr 4, 2016 at 14:20
  • in other words, how can i replace from parentChildCase.get(cMap.get(c).ID).add(cMap.get(c)); to this par.get(cMap.get(c).Parentid);
    – Nick
    Commented Apr 4, 2016 at 14:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .