1

I was hoping I could get assistance with this error I'm receiving when my Apex classes are running.

Failed First error: Attempt to de-reference a null object

I couldn't find a specific object that would be null in my code and wasn't sure how I could trace it to a line in my code.

In summary I have an active process builder that sends variables to an invokable method, which then runs another class to call out to an external API.

HELPER

global class InfluitiveEventsAPIHelper {
    @InvocableMethod
    //Method that accepts the ContactIDs from Process Builder and process through Influitive's API
    global static void updateContactStateInfo(List<EventsRequest > eventRequest) {
        Id recordId= eventRequest[0].recordId;
        String event_type= eventRequest[0].event_type;
        String points= eventRequest[0].points;
        String stagecode= eventRequest[0].stagecode;

        Contact con = [SELECT Id FROM Contact WHERE Id=: recordId];
        System.debug('updateContactStateInfo, con=' + con);
        Influitive_EventsAPI_Post.postEvent(con.Id, points, event_type, stagecode);

    }

    global class EventsRequest {
        @InvocableVariable(required=true)
        global ID recordId;

        @InvocableVariable(required=true)
        global String event_type;

        @InvocableVariable(required=true)
        global String points;

        @InvocableVariable(required=true)
        global String stagecode;

    }
}

CALLOUT CLASS

global class Influitive_EventsAPI_Post {

    public static HttpCalloutMock mock = null; // this is populated while call is from test class

    public class Message {
        public Event event;  // Event information

    }

    //Event information
    public class Event{
        public Contact contact;
        public String points;
        public String type;
        public Stage stage;

    }

    public class Contact {
        public String id;
    }

    public class Stage {
        public String code;
    }

    // this method convert the message object to the JSON string which is used to send information
    // to the Influitive's referral API in a post request
    private static String toJsonString(Message m) {
        return JSON.serializePretty(m.event);
    }    

    /**
     * it will send the updated event information to the Influitive API.
     **/
    @future (callout=true)
    public static void postEvent(Id objectId, String points, String type, String stagecode) {

        Event e = new Event();

        e.contact.id = objectId;
        e.points = points;
        e.type = type;
        e.stage.code = stagecode;

        //retrive Authorization and X Org ID from Custom settings.
        ReferralSettings__c authSettings = ReferralSettings__c.getValues('Authorization');
        ReferralSettings__c xOrgIdSettings = ReferralSettings__c.getValues('X_ORG_ID');

        System.debug('authSettings =' + authSettings  + 'xOrgIdSettings=' + xOrgIdSettings );

        // Instantiate a new HTTP request, specify the method (PUT) as well as the endpoint  
        String url='https://api.influitive.com/events';
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint(url);
        //req.setHeader("X-HTTP-Method-Override","POST");
        req.setMethod('POST');
        req.setHeader('Authorization', authSettings.Value__c);
        req.setHeader('Content-Type', 'application/json');
        req.setHeader('X_ORG_ID', xOrgIdSettings.Value__c);



        Message m = new Message();
        m.event = e;
        System.debug(m);



        String body = toJsonString(m);
        req.setMethod('POST');
        req.setBody(body);
        System.debug('this is the body');
        System.debug(body);

        HttpResponse res = null;

        if (Test.isRunningTest() && (mock!=null)) {
           res = mock.respond(req);
        } else {
            res = h.send(req);
        }

        System.debug('Response: '+ res.getBody());
        System.debug('Response: '+ res.getStatusCode());
        String sInteger = String.valueOf(res.getStatusCode());

       // Add notes to contact

        note n = new note();
        n.parentId= objectId; //Id of the object for which you want to add this note
        n.body=sInteger + url + authSettings.Value__c + xOrgIdSettings.Value__c; // body of the note. this should be populated from the notes field that you have shown in the screenshot
        n.title = 'Response from Influitive API at ' + datetime.now(); // the title of the note
        n.isPrivate=false;  // if private it will be visible only for the owner and any user with modify all permission
        insert n; //insert the record

    }
}

TRACE

System.NullPointerException: Attempt to de-reference a null object

Class.Influitive_EventsAPI_Post.postEvent: line 47, column 1

This is line 47 of my code:

e.contact.id = objectId;
  • Please edit your post to clarify which line is 47, as it appears to be a line which could not throw this error: ReferralSettings__c authSettings = ReferralSettings__c.getValues('Authorization');. – Adrian Larson Oct 12 '17 at 16:24
1

Set the lookup using the field. Its API Name is ContactId.

e.ContactId = objectId;

When you use e.Contact.Id instead, what you are doing is first retrieving e.Contact, then trying to set a value on it. That wouldn't work for setting the relationship even if this related record weren't null. Here's an equivalent version of what you currently have:

Event e = new Event();
Contact relatedContact = e.Contact; // this value is null
relatedContact.Id = someValue; // here you try to de-reference null.Id

See also:

1

I resolved the issue as I wasn't instantiating the nested objects.

this did the trick:

//Event information
public class Event{
    public Contact contact = new Contact();
    public String points;
    public String type;
    public Stage stage = new Stage();

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.