1

I have a map here mapOfQuesToAnsToYearToMaleOrFemaleCount and I am trying to get count here from test class, calling from VF Page I am able to achieve this but not in test class. from test class I am getting my map(mapOfQuesToAnsToYearToMaleOrFemaleCount) like below but not able to get the value of count, can any body tell me what is the problem?

Integer count = mapOfQuesToAnsToYearToMaleOrFemaleCount.get(questionID).get(answerId).get(year).get(gender); //trying to get value of count 

system.debug(mapOfQuesToAnsToYearToMaleOrFemaleCount); 
// {a045E000001Bs6NQAS={a005E000002My8CQAS={year 07={Female=0, Male=0}}}, a045E000001Bs6OQAS={a005E000002My8DQAS={year 07={Female=0, Male=0}}, a005E000002My8EQAS={year 07={Female=0, Male=0}}}} //map I got from running test

system.debug(questionID);
//a045E000001Bs6NQAS

system.debug(answerId);
// a005E000002My8CQAS

system.debug(year);
//year 07

system.debug(gender);
//Female

system.debug(count);
//null
3
  • Because count is null in the debug output
    – Eric
    Commented Apr 17, 2017 at 7:27
  • @Eric Why it is null if I am getting my map a045E000001Bs6NQAS={a005E000002My8CQAS={year 07={Female=0, Male=0}}} like this and male and female count 0. Commented Apr 17, 2017 at 7:37
  • = Unless you are summing Male and female if you are getting any other value it does not exist so it will be null. All you can get is year, male, female. Anything else will be null because as you can tell from the debug that is all there is
    – Eric
    Commented Apr 17, 2017 at 12:35

1 Answer 1

0

Can you copy how you have defined your Map object. Seems like the way your are passing values to Map to fetch value is not correct that's why it's returning a Null.

I looked at the Map Valued object that you posted and the structure doesn't look ok to me.

I wrote below sample code and it works perfectly for me.

Map<String,Map<String,Map<String,Map<String,Integer>>>>
    mapObject=new Map<String,Map<String,Map<String,Map<String,Integer>>>>();
Map<String,Integer> gender=new Map<String,Integer>();
gender.put('Female',0);
gender.put('Male',0);

Map<String,Map<String,Integer>> year=new Map<String,Map<String,Integer>>();
year.put('year 07',gender);

Map<String,Map<String,Map<String,Integer>>> answers=new Map<String,Map<String,Map<String,Integer>>>();
answers.put('a005E000002My8CQAS',year);

Map<String,Map<String,Map<String,Map<String,Integer>>>> questions=new Map<String,Map<String,Map<String,Map<String,Integer>>>>();
questions.put('a045E000001Bs6NQAS',answers);

System.debug(questions);

Integer count=questions.get('a045E000001Bs6NQAS').get('a005E000002My8CQAS').get('year 07').get('Female');

System.debug('count is '+count);
2
  • Yes, my map is exactly like yours but I m not able to get it with test class. I have got count value in controller class while calling from VF page. it works perfectly there but in test class it's not getting the value and giving me null yet I have values for male n female that is 0. Commented Apr 17, 2017 at 8:57
  • And yes @bhawna I cannot pass the values directly as I am iterating on results. Commented Apr 17, 2017 at 9:23

You must log in to answer this question.

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