1

I'm writting a test class for a rest class, and I'm getting a

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

and

Class.RESTCommande.getCodeCommande: line 28, column 1 Class.RESTCommandeTest.getCodeCommande_Test: line 48, column 1

with which I remain blocked.

Here is a part of my Rest class :

@RestResource(urlMapping='/comm')
global with sharing class RESTCommande  {

@HttpGet
global static String getCodeCommande() {
    String retour;
    RestRequest req = RestContext.request;
    String eventId = req.params.get('eventId');
    Event comm;

    RecordType rtWeb = [SELECT Id
                                FROM RecordType
                                WHERE Name = 'Web'
                                AND SobjectType = 'Commande__c'
                                LIMIT 1];

    if(eventId == null){
        try {
            insert new Commande__c( Cons__c = comm.WhatId, RecordTypeId = rtWeb.Id);
        } catch (DMLException e) {
            return null;
        }

    }
    else{
      //some code
    }
  }

the line 28 is the one with insert new Commande__c( Conseill_re__c = commandeWeb.WhatId, RecordTypeId = rtWeb.Id);

My test class :

@isTest
private class RESTCommandeTest {

@isTest static void getCodeCommande_Test() {
    Account c= new Account(FirstName = 'test',
                                      LastName = 'test');
    insert c;
    Contact h= new Contact(Firstname = 'test',Lastname = 'test', Salutation = 'Madame');
    insert h;
    Event ev = new Event(StartDateTime = DateTime.now(),
                         EndDateTime = DateTime.now(),
                         Subject = 'Appel',
                         WhoId = h.Id,
                         WhatId = c.Id);
    insert ev;


    RestRequest req = new RestRequest();
    req.requestURI = 'https://cs86.salesforce.com/services/apexrest/comm?eventId=' + ev.Id;  
    req.httpMethod = 'GET';

    RestContext.request = req;

    RecordType rt= [SELECT Id
                                FROM RecordType
                                WHERE Name = 'Autre type'
                                AND SobjectType = 'Commande__c'
                                LIMIT 1];
    Commande__c comm = new Commande__c(H__c = h.Id,
                                        RecordTypeId = rt.Id,
                                        Cons__c = c.Id);
    String code = RESTCommande.getCodeCommande();

}


}

the error appear at the line with : String code = RESTCommande.getCodeCommande();

  • in ur insert statement, you are using Cons__c = comm.WhatId but you have not initialised or assigned anything to the variable comm. you just declared it and using it directly which is causing the issue. – Vamsi Krishna Jun 2 '16 at 13:02
3

Here are the reason of null pointer exception. Your event variable

Event comm;  // Can be initilized as Event comm=new Event ();

is not initialized in main code class. So you are not allowed to write a code line similar to one

 insert new Commande__c( Cons__c = comm.WhatId, RecordTypeId = rtWeb.Id);

because you can not refer whatid of a null variable.

The reason exception line was reached is because you were not able to set Event in RestRequest that resulted eventId to be null and finally reaching exception code.

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.