1

I have below code where I am getting a null reference error. I can't understand why my if clause shows faulty.

for(List<hierarchyNode> hierLists: hier)
            for(hierarchyNode hierNode: hierLists)
                if(hierNode.nType != 'Terr' && users.get(hierNode.UserId).Name!=null) // This line throws error
                    hierNode.Value += ' ' + users.get(hierNode.UserId).Name;

1 Answer 1

2

This is because if users.get(hierNode.UserId) this map return null then you will get a null pointer exception.

You should update your if condition and check the value first

if(hierNode.nType != 'Terr' && users.containsKey(hierNode.UserId) && users.get(hierNode.UserId).Name!=null)
2
  • 1
    I will accept the answer in 9 mins as forum throws a validation for 9 mins minimum to accept an answer. Thanks for your quick help. :) Commented Dec 2, 2016 at 9:34
  • 1
    @SFDC_Learner glad I can help. Commented Dec 2, 2016 at 10:30

You must log in to answer this question.

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