Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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/rashmi' ;

    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 of HttpGet method 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 showing that note1 is null how is it possible?please help how to remove this null pointer exception and when i call this rest service it is not returning any error but a response .is some problem or exceptional behaviour of List in Test when called from TestClass??please someone help!!

share|improve this question

2 Answers 2

Are you sure you're looking at the correct line for the error? I would look at whether you're trying to access note after your conditional.

The SOQL query should always return a list, so note1 shouldn't be Null. But, if the query returned zero rows, note won't be assigned and could throw a Null Pointer Exception if you attempt to dereference it.

share|improve this answer
    
That makes the most sense -- note1 is not null, but is empty; however note is null. The original code in the post was horribly formatted and all of that logic was on one line (@Mohith kindly formatted it better), so Ritesh probably confused note and note1 when describing which was null. –  Peter Knolle Jan 27 '13 at 17:40
    
@PeterKnolle oops may be the confusion between note and note1 led me to understand wrongly.if(note1 !=null ){ if(note1.size() >0) note=note1.get(0); }this is not making any sense to me –  Mohith Shrivastava Jan 27 '13 at 18:11

You need not check for both the condition.

Just check for

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

 Notification_Settings__c note;

Also in the above line i will instantiate the object before populating it .

Notification_Settings__c note=new Notification_Settings__c();
note=note1.get(0);

This should fix the issues

share|improve this answer
    
I would think that the assignment from note1.get(0) to a null note reference wouldn't cause an NPE. It would have to attempt to de-reference it for that to occur (e.g., note.property__c). –  Peter Knolle Jan 27 '13 at 17:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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