+ Start a Discussion

List does not receive values

Hello there


I'm at my first Apex coding project. When testing my code, I receive errors saying that an insert failed because I tried to de-reference a null-pointer. In the Debug Log I found a section that is, in my opinion, the source for this null pointer exception.


In the code below, I have a method to add my Handler with its corresponding action. The method is called from a trigger which creates a new instance of my Handler and uses it along with the action as parameters.


The handler list is not yet initiated and this is done by passing it a list of Handlers out of the actionHandlerMapping. Usually actionHandlerMapping is empty upon first run of this method and so also handlerList is empty. That's why the handlerList is added the Handler passed by the method and the actionHandlerMapping is set a key-value pair with the name of the action and the list of handlers.



*	Add handler to list of action type. Handler is an interface that my Apex class implements to handle certain
*	actions of a trigger. TriggerEvent is an Enum. actionHandlerMapping is a Map<String, Handler>

public void addOneHandler(Handler theHandler, TriggerEvent theEvent)
	List<Handler> handlerList = actionHandlerMapping.get(theEvent.name());
	if (handlerList == null)
		handlerList = new List<Handler>();
		actionHandlerMapping.put(theEvent.name(), handlerList);

Now in the "Variables" window of the Developer Console on Salesforce in the debug log, the list handlerList is always null. I have no clue why this list remains null even though I add a Handler object to it. The Handler Object itself exists as I can see on the Variables window, so I am not actually adding a null value.


Any help appreciated.



I think the problem is in the below line:


List<Handler> handlerList = actionHandlerMapping.get(theEvent.name());


u need to check

List<Handler> handlerList  = null;


if (actionHandlerMapping != null && actionHandlerMapping.containsKey(theEvent.name()))

        handlerList  = actionHandlerMapping.get(theEvent.name());


because if the "actionHandlerMapping" is null and not contain the key which u want to get then it gives error .



Please let me know if u have any problem on same and if this post helps u please throw KUDOS by click on star at left.