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
Steve CairneySteve Cairney 

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

Hi all, I'm trying to delpoy the Round Robin Assigmnet app that I've tailored to work with leads.

 

Unfortunatly, the test class is failing 1 out of 4 and this means I can't deploy to my production, can you guys help?

 

Here's the whole test class and I've highlighted the line referenced in the error (Class.TestMethodClsLeads1.myTest3: line 217, column 1) in bold. Any ideas?

 

@isTest
private class TestMethodClsLeads1 {

    static testMethod void myTest1() {

        // This code runs as the system user

        User u1;

        try{
          u1 = [select Id from User WHERE IsActive=True AND Profile.Name = 'System Administrator'  LIMIT 1];
        } catch (QueryException qe){

        List<User> users = [SELECT Id, Profile.PermissionsModifyAllData FROM User WHERE IsActive = true LIMIT 1000];

        for(User u : users){
            if(u.Profile.PermissionsModifyAllData = true){
              u1 = u;
              break;
        }
        }

        }

        System.debug(u1);

       //*****Create Queue 
       
       Group testGroup = new Group ();
       testGroup.Name = 'TestQueue';
       testGroup.Type = 'Queue';
       insert testGroup;
       
       QueueSObject testQueue = new QueueSObject();
       testQueue.QueueId = testGroup.id;
       testQueue.SObjectType = 'Lead';
       insert testQueue;

       // Second Queue       
       Group testGroup2 = new Group ();
       testGroup2.Name = 'TestQueue2';
       testGroup2.Type = 'Queue';
       insert testGroup2;
       
       QueueSObject testQueue2 = new QueueSObject();
       testQueue2.QueueId = testGroup2.id;
       testQueue2.SObjectType = 'Lead';
       insert testQueue2;


       test.starttest();
        
        //Run test

        //Assign Lead with out any Assignment Groups
        Lead c1 = new Lead (Firstname='testLead', Lastname='testLead', Company='test', Phone='5555555555', Email='test@tester.com',TempOwnerID__c=testGroup2.id, OwnerID=u1.id); //TempOwnerID__c=testGroup2.id,  
        insert c1;
        update c1;
        
        
        //Create Assignment Group
        Assignment_Group_Name__c ag1 = new Assignment_Group_Name__c (Name='TestAG', Type__c = 'Lead');
        insert ag1;

         
        //Add bad queue name
        Assignment_Group_Queues__c agqBad = new Assignment_Group_Queues__c(name='Bad Queue',Assignment_Group_Name__c = ag1.id );

        try {
            insert agqBad; 
        } catch (DmlException e){
             System.assert(e.getMessage().contains('CUSTOM_VALIDATION_EXCEPTION'), e.getMessage()); 
        
        } //catch

        test.stoptest();
        
    }
    
    static testMethod void myTest2() {

        // This code runs as the system user

        User u1;

        try{
          u1 = [select Id from User WHERE IsActive=True AND Profile.Name = 'System Administrator'  LIMIT 1];
        } catch (QueryException qe){

        List<User> users = [SELECT Id, Profile.PermissionsModifyAllData FROM User WHERE IsActive = true LIMIT 1000];

        for(User u : users){
            if(u.Profile.PermissionsModifyAllData = true){
              u1 = u;
              break;
        }
        }

        }

        System.debug(u1);

       //*****Create Queue 
       
       Group testGroup = new Group ();
       testGroup.Name = 'TestQueue';
       testGroup.Type = 'Queue';
       insert testGroup;
       
       QueueSObject testQueue = new QueueSObject();
       testQueue.QueueId = testGroup.id;
       testQueue.SObjectType = 'Lead';
       insert testQueue;

       // Second Queue       
       Group testGroup2 = new Group ();
       testGroup2.Name = 'TestQueue2';
       testGroup2.Type = 'Queue';
       insert testGroup2;
       
       QueueSObject testQueue2 = new QueueSObject();
       testQueue2.QueueId = testGroup2.id;
       testQueue2.SObjectType = 'Lead';
       insert testQueue2;


       test.starttest();
        
        //Run test
        
        //Create Assignment Group
        Assignment_Group_Name__c ag1 = new Assignment_Group_Name__c (Name='TestAG', Type__c = 'Lead');
        insert ag1;        

        //Add Good Queue to Assignment Group
        Assignment_Group_Queues__c agq1 = new Assignment_Group_Queues__c(name=testGroup.Name ,Assignment_Group_Name__c = ag1.id );
        insert agq1;
        
        
        //Add User to Assignment Groups Users
        Assignment_Groups__c agu1 = new Assignment_Groups__c (User__c = u1.id, Active__c='True', Group_Name__c = ag1.id, Last_Assignment__c = datetime.valueOf('2009-01-01 21:13:24') );
        insert agu1;

        Lead c2 = new Lead (Firstname='testLead', Status='New', Lastname='testLead', Company='test', Phone='5555555555', Email='test@tester.com', TempOwnerID__c=testGroup2.id , OwnerID=testGroup.id); //Set owner ID to Queue
        insert c2;
        update c2;        

        test.stoptest();
        
    }

    static testMethod void myTest3() {

        // This code runs as the system user

        User u1;

        try{
          u1 = [select Id from User WHERE IsActive=True AND Profile.Name = 'System Administrator'  LIMIT 1];
        } catch (QueryException qe){

        List<User> users = [SELECT Id, Profile.PermissionsModifyAllData FROM User WHERE IsActive = true LIMIT 1000];

        for(User u : users){
            if(u.Profile.PermissionsModifyAllData = true){
              u1 = u;
              break;
        }
        }

        }

        System.debug(u1);
        
        User u2;

        try{
          u2 = [select Id from User WHERE IsActive=True AND Profile.Name = 'AMAG Standard User'  LIMIT 1];
        } catch (QueryException qe){
//should run an error test here like above.              
        }


        System.debug(u2);
        


       //*****Create Queue 
       
       Group testGroup = new Group ();
       testGroup.Name = 'TestQueue';
       testGroup.Type = 'Queue';
       insert testGroup;
       
       QueueSObject testQueue = new QueueSObject();
       testQueue.QueueId = testGroup.id;
       testQueue.SObjectType = 'Lead';
       insert testQueue;

       test.starttest();
        
        //Run test        
        
        //Create Assignment Group
        Assignment_Group_Name__c ag1 = new Assignment_Group_Name__c (Name='TestAG', Type__c = 'Lead');
        insert ag1;        

        //Add Good Queue to Assignment Group
        Assignment_Group_Queues__c agq1 = new Assignment_Group_Queues__c(name=testGroup.Name ,Assignment_Group_Name__c = ag1.id );
        insert agq1;
        
         //Add User to Assignment Groups Users
        Assignment_Groups__c agu1 = new Assignment_Groups__c (User__c = u1.id, Active__c='True', Group_Name__c = ag1.id, Last_Assignment__c = datetime.valueOf('2009-01-01 21:13:24') );
        insert agu1;   

        //Add User to Assignment Groups Users
Assignment_Groups__c agu2 = new Assignment_Groups__c (User__c = u2.id, Active__c='True', Group_Name__c = ag1.id, Last_Assignment__c = datetime.valueOf('2010-01-01 21:13:24') ); insert agu2; Lead c2 = new Lead (Firstname='testLead', Status='New', Lastname='test', Company='test', Phone='5555555555', Email='test@tester.com', OwnerID=testGroup.id); //Set owner ID to Queue insert c2; update c2; Lead c3 = new Lead (Firstname='testLead2', Status='New', Lastname='test', Company='test', Phone='5555555555', Email='test@tester.com', TempOwnerID__c=testGroup.id , OwnerID=testGroup.id); //Set owner ID to Queue insert c3; update c3; Lead c4 = new Lead (Firstname='testLead3', Status='New', Lastname='test', Company='test', Phone='5555555555', Email='test@tester.com', TempOwnerID__c=testGroup.id , OwnerID=testGroup.id); //Set owner ID to Queue insert c4; update c4; test.stoptest(); } static testMethod void myTest4() { // This code runs as the system user User u1; try{ u1 = [select Id from User WHERE IsActive=True AND Profile.Name = 'System Administrator' LIMIT 1]; } catch (QueryException qe){ List<User> users = [SELECT Id, Profile.PermissionsModifyAllData FROM User WHERE IsActive = true LIMIT 1000]; for(User u : users){ if(u.Profile.PermissionsModifyAllData = true){ u1 = u; break; } } } System.debug(u1); //*****Create Queue Group testGroup = new Group (); testGroup.Name = 'TestQueue'; testGroup.Type = 'Queue'; insert testGroup; QueueSObject testQueue = new QueueSObject(); testQueue.QueueId = testGroup.id; testQueue.SObjectType = 'Lead'; insert testQueue; test.starttest(); //Run test //Create Assignment Group Assignment_Group_Name__c ag1 = new Assignment_Group_Name__c (Name='TestAG', Type__c = 'Lead'); insert ag1; //Add Good Queue to Assignment Group Assignment_Group_Queues__c agq1 = new Assignment_Group_Queues__c(name=testGroup.Name ,Assignment_Group_Name__c = ag1.id ); insert agq1; //Test for AG-Queues already assigned to another Assignment Group -- trying to assign a group with a name that already exists.. repeats code above. Assignment_Group_Queues__c agq2 = new Assignment_Group_Queues__c(name=testGroup.Name,Assignment_Group_Name__c = ag1.id ); try { insert agq2; } catch (DmlException e){ System.assert(e.getMessage().contains('CUSTOM_VALIDATION_EXCEPTION'), e.getMessage()); } //catch test.stoptest(); } }

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Neha LundNeha Lund

instead of u2 try giving u1

All Answers

Neha LundNeha Lund

I guess u2 does not have value that means it is null .. that's why it is giving null pointer exception

Neha LundNeha Lund

instead of u2 try giving u1

This was selected as the best answer
Neha LundNeha Lund

instead of querying data.. you can also create the test data..

Steve CairneySteve Cairney

Thanks for the reply, I should point out that I have have SF development thurst upon me 2 weeks ago so you'll have to take it slow and explain :)

 

Do you mean change the lines to the following? (I've just changed User__c = u2.id)

 

//Add User to Assignment Groups Users
        Assignment_Groups__c agu2 = new Assignment_Groups__c (User__c = u1.id, Active__c='True', Group_Name__c = ag1.id, Last_Assignment__c = datetime.valueOf('2010-01-01 21:13:24') );
        insert agu2;

 EDIT: That worked!

 

What's the purpose of these in the test? Can I remove them? I have acceptable code coverage on the triggers...

Steve CairneySteve Cairney

Hey, I wonder if you could help me with the following:

 

I've just added a new trigger to the lead object:

 

trigger changeLeadOwner on Lead (before insert, before update) {

// no bulk processing; will only run from the UI
if (Trigger.new.size() == 1) {
for(lead le :trigger.new)
le.Ownerid =le.Phone_Lead_Owner__c;

}
}

 Unfortuntely the test posted above now throws a System.DmlException

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, changeLeadOwner: data changed by trigger for field Owner ID: owner cannot be blank

 I guess the problem here is that the original test is now looking for the new custom field on the lead? Can I remove it? The test class posted in the first post is for a queue, but the new trigger is manual input only.

 

Arrrrgh!