-1

The code is working. Just to make it in Production, I need to do proper unit test to make sure the code coverage is above 75%.

In the Controller Extension, I made a simple Page Reference to save the record and it uses update object.relatedObject__r to save the related object. Later in the Unit Test, it returns the error saying

Attempt to de-reference a null object.

The update line is controlled by condition that when object.relatedObject__c == null, the update line would never be reached.

Therefore, I am sure that the object.relatedObject__c is not null. Then, I thought maybe some fields in the relatedObject are null, which might cause this error. Then I put content in all the fields in the relatedObject. However, the system still returns the error

"Attempt to de-reference a null object".

How do I solve this problem? Is there a way to test on the "Update object.relatedObject__r" code?

Thanks a lot.

2
  • 1
    if you can share ur code, that will help the community to answer you better. also did you check if the object itself is not null ? – Vamsi Krishna Gosu Feb 23 '16 at 1:40
  • Do you ever set a value in object.RelatedObject__r? – Adrian Larson Feb 23 '16 at 3:19
0

You got me confused on using "__r and "__c", but just for clarification.

when you're using "__r", it means that you are referencing an object's child records.

If you're using __c, it means that you're using a custom field on an Object.

which means object.relatedObject__r is not equal to object.relatedObject__c

As for your condition, object.relatedObject__c == null, what you were trying to check if null is the custom field of your object.

It should be the child object list that you'll need to check if you don't want your object.relatedObject__r to be updated if it is null.

if(object.relatedObject__r != null) {
    update object.relatedObject__r;
}
6
  • Now I am sure the object.relatedObject__r is null now. That is why the error is happening. I would like to make object.relatedObject__r not null. How should I do? I have inserted one record of that relatedObject and connected it with the particular record of object. Why is still object.relatedObject__r null? Does it mean that there are fields in relatedObject that are null? – Yifei Pei Feb 23 '16 at 4:31
  • Yes, that's why you need to check first if object.relatedObject__r is null before you update it. – sfdx Feb 23 '16 at 4:33
  • Hi. Sorry, I think after I replied this comment, I edited that comment later. – Yifei Pei Feb 23 '16 at 4:34
  • If the __r is null, does it mean that there are fields still null in that relatedObject? – Yifei Pei Feb 23 '16 at 4:36
  • Can you paste a snippet of your code where you are checking if object.relatedObject__r is null – sfdx Feb 23 '16 at 4:38
1

Null pointer exception usually thrown when you apply some operations on a null object. Here in your case the object itself may be null, please check if the object has some value and update accordingly.

0

Sorry guys. I do not paste the code here because the code is too complicated that when you see a small chunk of code you will always want to see the larger picture to explain more. The test involves at least 5 major objects and some other supporting objects, which almost drove me crazy.

Now I am sure that the __r actually represents all the field of that related object. There should be no null field if I want to test on __r. In real execution, there wouldn't be any null field once the record is inserted ('' or 0 is not null). However, in testing, I have to manually add all the fields of that particular object. Further more, I have to focus on the objects structure to make sure the manual generation is correct and logical for the testing. For each of "object" and "relatedObject", I have more than 100 fields...

Anyway, I have sort of solved the problem and the part has been on Production now. I do suggest all testing have an initial function to prepare all the needed objects (although that might be the most complicated part in the unit test). If it is possible, I hope in the near future Salesforce can provide something like record generator for unit test usage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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