2

So I've done quite a bit of searching on the forum and am learning a ton. I really appreciate the experts on this forum who are willing to help out.

I am taking inspiration/following the code posted on: https://github.com/sfdcmatt/DF13ApexRest/blob/master/REST_AccountService_V7.cls but am running into an error.

I will be passing in one account and one custom object record. I'm trying to set a look-up field on a record when passing information to create both the account and the custom object. I'm getting an Error when passing in the params from workbench.

Error: Attempt to de-reference a null object

@RestResource(urlMapping='/v1/application/*')
global class REST_ApplicationService_V1 {  

@HttpPost
  global static PostResponseWrapper doPostMulti(RequestWrapper rqst) {
    RestRequest req = RestContext.request;
    RestResponse res = RestContext.response;
    PostResponseWrapper response = new PostResponseWrapper();

    try {
        insert rqst.acct;
        response.acct = rqst.acct;

        LLC_BI__Loan__c myLoan = new LLC_BI__Loan__c();   
        myLoan.LLC_BI__Account__r.Id = rqst.acct.Id;
        insert rqst.myLoan;
        response.myLoan = rqst.myLoan;

       //Additional lines etc 
       }
catch(Exception exc) {
        res.StatusCode = 500;
        response.acct = null;
        response.myloan = null;
        response.status = 'Error';
        response.message = 'Your request failed with the following error: ' + exc.getMessage();
    }

    return response;
}
global class RequestWrapper {
    Account acct;
    LLC_BI__Loan__c myLoan;       
}

global class PostResponseWrapper {
    Account acct;
    LLC_BI__Loan__c myLoan;
    String status;
    String message;

    public PostResponseWrapper(){
  }  
  • 1
    Do you have a line number? Can you tell us which line is actually throwing the error? – Adrian Larson May 19 '16 at 20:41
  • I don't get an error in the Developer Console, however, when I try and input data via workbench I get the error message: "message" : "Your request failed with the following error: Attempt to de-reference a null object", I'm pretty sure it has to with the setting of the variable myLoan. – Starley Flynn May 19 '16 at 20:45
  • 1
    Best (for debugging purposes) to append exc.getStackTraceString() as well as exc.getMessage() so you get line numbers. – Keith C May 19 '16 at 20:54
4

A reference field like:

myLoan.LLC_BI__Account__r

is only populated when the two objects are queried together; in your code that reference will be null and so a likely source of the error.

Replace these lines:

    LLC_BI__Loan__c myLoan = new LLC_BI__Loan__c();   
    myLoan.LLC_BI__Account__r.Id = rqst.acct.Id;
    insert rqst.myLoan;

with:

    rqst.myLoan.LLC_BI__Account__c = rqst.acct.Id;
    insert rqst.myLoan;

which is the conventional way of relating two objects, where you set the foreign key ID field to the related objects's ID. This assumed both the Account and LLC_BI__Loan__c are set by the caller in the request.

  • Um, you seem to have removed the object instantiation in your replacement block. – Adrian Larson May 19 '16 at 20:52
  • @AdrianLarson The question says both object are passed into the REST method; in any case the local variable myLoan was not being inserted or otherwise used. – Keith C May 19 '16 at 20:57
  • Ah gotcha. It's a little confusing to look at but it makes sense now. – Adrian Larson May 19 '16 at 20:59
  • Thanks so much!! I don't have enough reputation to up-vote yet, but that fixed the issue. – Starley Flynn May 19 '16 at 21:11
  • @StarleyFlynn - but you can mark the answer as the solution , I'm sure Keith and the Community will appreciate it. – cropredy May 20 '16 at 1:00

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.