function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ritesh__Ritesh__ 

Unexpected Null Pointer exception in testing

i am writing a unit test of a rest service in apex

My Test Class is

global class Feedpost9 {

    // your methods here...

    static testMethod void testRest() {
        // set up the request object
        System.RestContext.request = new RestRequest();
        RestContext.request.requestURI = '/v.9/notifications/preferences/ritesh';
        // Invoke the method directly
        Member__c member=new Member__c(name='ritesh');
        insert member;

        Notification_Settings__c no=new Notification_Settings__c(member__c=member.Id);
         no.Event__c='Category|Cloud Foundry' ;
         no.Event_Per_Member__c='12';
         insert no;


        NotificationRestService.retrievingNotificationSettings();

        RestContext.request.requestURI = '/v.9/notifications/preferences' ;

        NotificationRestService.retrievingNotificationSettings();
        RestContext.request.requestURI ='/v.9/notifications/preferences/ritesh.xml';
NotificationRestService.retrievingNotificationSettings();
    }
}

when test unit reach at this point then i got an error RestContext.request.requestURI = '/v.9/notifications/preferences' ;

        NotificationRestService.retrievingNotificationSettings();

error is

System.NullPointerException: Attempt to de-reference a null object Class.NotificationRestService.retrievingNotificationSettings: line 46, column 1 Class.Feedpost9.testRest: line 23, column 1

and the segment where i am getting this error is

 List<Notification_Settings__c> note1=[SELECT id,name,Member__r.name,Do_not_Notify__c,Event__c,Event_Per_Member__c,Notification_Method__c from Notification_Settings__c WHERE Member__r.name= :userName Limit 1];
Notification_Settings__c note;

if(note1 !=null )
{if(note1.size() >0)
note=note1.get(0);
}

and error is on the line {if(note1.size() >0) i am unable to figure if note1 is not equal to null then it will come to next line but in next line it is giving me error why note1 is null in next linel and passing first if statement.query should return empty list .please someone help!!

 

sivaextsivaext

Hi 

 

 Null pointer exception means, the query returns 0 records. according line 42 , the list returns 0. 

  

  Check you debug log and see values of member__r.name and username 

TrUs3Uw3TrUs3Uw3

Hi Ritesh__,

 

My assumption is that line

NotificationRestService.retrievingNotificationSettings();

 makes a callout which is prohibited after insert on line

insert member;

 I have the same problem and I don't know how to solve it ;-(