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
VaderVader 

APEX Test Method Error

I am trying to get better coverage of a particular controller extension and am having a few issues with one particular line.  I have a custom object where I store a task Id and a user Id and a few other fields.  This class is meant to query, insert, or delete records in that object.

 

In the getInformedUsers method below, all the lines test fine with the exception of the Return property.  I keep getting a Attempt to dereference a null object error.  The only other place where I can't seem to get coverage is in the removeInformed method for the redirect lines.  

 

I am currently at 61% pass if I do not test the getInformedUsers method but would love figure out how to cover that as well.  Code below:

 

public with sharing class informedUserList {
	
	private ApexPages.StandardController controller;
	
	
	public informedUserList(ApexPages.StandardController stdController) {     
      controller = stdController;
   }	   
	public List<Task_Informed_Users__c> getInformedUsers() {
		
		String tskID = ApexPages.currentPage().getParameters().get('Id');
		String longTaskId = String.valueOf(tskId);
		String shortTaskId = longTaskId.left(15);	
		     
        return [SELECT id, Informed_User__r.Name, Chatter_Notification__c, Send_Email__c FROM Task_Informed_Users__c WHERE Task_Id__c =: shortTaskId];
    }   
    
	public PageReference addInformed() {		
		string tskId = ApexPages.currentPage().getParameters().get('Id');		
		
		PageReference tskInformed = new pagereference('/apex/informedUsers?tId='+tskId);
		tskInformed.setRedirect(true);
  		return tskInformed;  			

	}

	public PageReference removeInformed() {		
		string tskId = ApexPages.currentPage().getParameters().get('Id');
		Id iuRecId = System.currentPageReference().getParameters().get('iuRec');
		
		delete [select id from Task_Informed_Users__c where ID=: iuRecId ]; 

   		return null;
		
		PageReference tskInformed = new pagereference('/apex/taskDetail?Id='+tskId);
		tskInformed.setRedirect(true);
  		return tskInformed;  			

	}
	
    
    public static Task testTsk;
    public static Task_Informed_Users__c testInformedUser;
    public static ID testTaskId;
    public static User testUserId;
        
    static testMethod void informedUserListTest() {    	
    	Test.startTest();
    	
    	testUserId = [SELECT ID FROM User WHERE LastName=: 'User2'];
        testTsk = new Task(Subject='testTask', ownerId=testUserId.Id); 
        insert testTsk;
        testTaskId = testTsk.Id;
            
        testInformedUser = new Task_Informed_Users__c(Informed_user__c=testUserId.Id, Task_ID__c=testTaskId, Chatter_Notification__c=FALSE, Send_Email__c=FALSE);
        insert testInformedUser;
        
        Test.setCurrentPage(Page.taskDetail);
		ApexPages.currentPage().getParameters().put('tId',testTaskId);         
              
        ApexPages.StandardController con = new ApexPages.StandardController(testInformedUser);
        informedUserList tInformedUserList = new informedUserList(con); 
        
        tInformedUserList.addInformed();  //Tests Fine
      
        tInformedUserList.removeInformed(); //Tests all but the redirect code    
        
        tInformedUserList.getInformedUsers();  //Get Attempt to DeReference a Null Object error
           
        
        Test.stopTest();        
    }
      
}

 

Best Answer chosen by Admin (Salesforce Developers) 
crop1645crop1645

Looks like a typo to me

 

public List<Task_Informed_Users__c> getInformedUsers() {
		
		String tskID = ApexPages.currentPage().getParameters().get('Id');
		String longTaskId = String.valueOf(tskId);
		String shortTaskId = longTaskId.left(15);	
		     
        return [SELECT id, Informed_User__r.Name, Chatter_Notification__c, Send_Email__c FROM Task_Informed_Users__c WHERE Task_Id__c =: shortTaskId];
    }

 references queryparm 'Id'

 

but your testmethod:

Test.setCurrentPage(Page.taskDetail);
		ApexPages.currentPage().getParameters().put('tId',testTaskId);         
              

 uses 'tId'

 

Hence your 

String shortTaskId = longTaskId.left(15);

 fails as you can't do left() on null

All Answers

arizonaarizona

I think you need to change the order.  You are removing the user and then searching for the remove user.

 

tInformedUserList.removeInformed(); //Tests all but the redirect code    
        
tInformedUserList.getInformedUsers();  //Get Attempt to DeReference a Null Object error

Swap these two lines and you should have it.

crop1645crop1645

Looks like a typo to me

 

public List<Task_Informed_Users__c> getInformedUsers() {
		
		String tskID = ApexPages.currentPage().getParameters().get('Id');
		String longTaskId = String.valueOf(tskId);
		String shortTaskId = longTaskId.left(15);	
		     
        return [SELECT id, Informed_User__r.Name, Chatter_Notification__c, Send_Email__c FROM Task_Informed_Users__c WHERE Task_Id__c =: shortTaskId];
    }

 references queryparm 'Id'

 

but your testmethod:

Test.setCurrentPage(Page.taskDetail);
		ApexPages.currentPage().getParameters().put('tId',testTaskId);         
              

 uses 'tId'

 

Hence your 

String shortTaskId = longTaskId.left(15);

 fails as you can't do left() on null

This was selected as the best answer
VaderVader

@

 

I thought the same thing initially, but I tested removing the "remove" method from code as well as switching the order of execution in the test method and had the same result which seemed odd to me as well.

 

@