+ Start a Discussion

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' ;
         insert no;


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

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

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


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)

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!!





 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 


Hi Ritesh__,


My assumption is that line


 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 ;-(