0

I am attempting to handle exceptions within the context of using a custom object that we have to display error messages. I want to capture any exception or dmlexception messages and use them in the error we are displaying to the user but I only want to handle these if the Exception or DMLException object is not null.

I am getting the following error only when running unit tests:

System.NullPointerException: Argument 2 cannot be null

Here is the line of code that is generating this error:

getErrMsg('CMG_42', null, null);

And here is the function I am using to handle the display of the errors from our custom object:

public void getErrMsg(string errCode, Exception e, DMLException de){
        system.debug('DEBUG: err msg map size:' + errMsgMap.size());
        system.debug('DEBUG: err msg map --->' + errMsgMap);        

        if (errMsgMap.size() == 0){
            system.debug('DEBUG: err msg map is empty, filling it....');
            errMsgMap = utils.LoadErrorMessages();
        }

        if (e != null){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,errMsgMap.get(errCode)+e.getMessage()));
        }

        else if (de != null){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,errMsgMap.get(errCode)+de.getMessage()));
        }

        else{
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,errMsgMap.get(errCode)));
        }
    }

I am not sure why the unit testing is giving me this error.

3
  • Have you checked the class and line number the problem happens in? The stack trace will normally show which line/class inside getErrMsg is causing the problem.
    – Keith C
    Commented Mar 29, 2016 at 17:33
  • @KeithC Yes, I have added the function call that is causing the issue in the question above.
    – Geowil
    Commented Mar 29, 2016 at 17:51
  • I would expect the stack trace to have an exact line number.
    – Keith C
    Commented Mar 29, 2016 at 17:54

1 Answer 1

1

The error is telling you that the 2nd param you're sending into a method is null and it is not allowed to be.

Given the code that you're showing, it's most likely the parameters you're sending to create the new page message.

new ApexPages.Message(ApexPages.Severity.ERROR, errMsgMap.get(errCode))

It looks like errMsgMap.get(errCode) is null. Should be pretty easy to confirm this and handle it in your code.

1
  • Hi Mark, yes. This seems to be the issue when testing but when I actually physically test the functionality of the vf page there are no issues and I am seeing the correct errors being displayed.
    – Geowil
    Commented Mar 29, 2016 at 18:25

You must log in to answer this question.

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