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
Justin LJustin L 

Yet another SystemNullPointerException: Attempt to de-reference a null object

Hello Everyone,

    I've been working in Apex for about 2 days now, and our IT department has set a requirement for 100% test coverage.  I created a class/controller that allows us to clone an object with it's custom children.  I have several similar controllers for cloning other objects, but that was before the test requirement.  

   Anyways, I've been working on a test class for the controller, and have been running into tons of little errors.  So far I have been able to clear them up on my own, but now I'm hitting one that is throwing me off.  

Now I'm getting the attempt to de-reference error on a line that gets the URL from the cloned object.  
 
//switch to test
                Test.startTest();
        
        //call the method
                PageReference ref = ext.cloneWithItems();
        
        //create the matching page reference
                PageReference redir = new PageReference('/'+ext.newRecordId+'/e?retURL=%2F'+ext.newRecordId);
        
        //sending to specific url
                System.assertEquals(ref.getURL(),redir.getURL());
        
        //check that GBU Activity was created 
                GBU_Activities__c newGBU = [select id from GBU_Activities__c where id = :ext.newRecordId];
                                               System.assertNotEquals(newGBU, null);
        
        //stop test
                Test.stopTest();

Any thoughts on how I can rewrite the assert statement to validate the clone?

Thank you.
Best Answer chosen by Justin L
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

I am assuming that the method cloneWithItems returns a PageReference with a url of the cloned object when the process succeeds and probably if you have any sort of validation inside this method, when it fails an error message is added to ApexPages returning null. If that is the case and your method is returning an error that is why your PageReference is null. I recommend you to check why is getting an error and again if that is the case.

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
 

All Answers

Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

I am assuming that the method cloneWithItems returns a PageReference with a url of the cloned object when the process succeeds and probably if you have any sort of validation inside this method, when it fails an error message is added to ApexPages returning null. If that is the case and your method is returning an error that is why your PageReference is null. I recommend you to check why is getting an error and again if that is the case.

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
 
This was selected as the best answer
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Have you solved your problem?

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.