You need to sign in to do that
Don't have an account?
Test class error help
I have a unit test as follows:
On running test i am getting the following error: "System.NullPointerException: Attempt to de-reference a null object"
Not able to figure out where exactly the problem is. Please help!
public class projecttrigger {
|
Ah, formula fields are not calculated until insertion into the DB, and the object is not updated with those new values. You'll have to query the record again after inserting it to ensure that your formula fields are populated.
All Answers
if (i <= (prj.No_of_months__c + 1)) {
I don't see No_of_months being set anywhere, it's probably equal to null and when you try to add 1 to it... NPE.
No_of_months__c is a formula field. Can I set it anywhere, as it gives an error that this field is not writable?
Ah, formula fields are not calculated until insertion into the DB, and the object is not updated with those new values. You'll have to query the record again after inserting it to ensure that your formula fields are populated.
Can u help me with that? If required I can show my code through a team viewer session. Please let me know.
after insert prj;
You'll want to do prj = [SELECT id, name, project_name__c, country__c, end_market_segment__c, start_date__c, endDate__c, analysis_dell__c, projectValue__c, No_of_months__c, project_value_per_month__c FROM Project1__c WHERE id = :prj.id];
I have country_c as a lookup field in Project page. I have initialized the country_c as follows:
prj.country_c ='a0GD000000U6Y8P';
On deploying the test failed and it gave the following error:
Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: [Country__c]", Failure Stack Trace: "Class.projecttrigger.ProjectAnalysistrigger: line 18, column 9 External entry point"
Please help.
I could figure out the mistake that i was doing. For country_c I gave the id value of country record in sandbox. When it is deployed it was not detecting the record id, as it differs in production environment.