Note that there are some explanatory texts on larger screens.

plurals
  1. POSalesforce Apex test class with null pointer exceptions
    text
    copied!<p>I am trying to write a test method that requires me to generate an opportunity and all records associated with the opportunity. I keep getting null pointer exceptions in the strangest places.</p> <pre><code>public static void createOpp() { OpportunityEscalationtest.a = new Account(Name = 'SGC Test Account' , Type = 'Customer' , Phone = '(00) 0000 0000'); insert OpportunityEscalationtest.a; OpportunityEscalationtest.c = new List&lt;Contact&gt;(); Contact newC = new Contact( FirstName = 'Jack' , LastName = 'O\'Neil' , Phone = '(00) 0000 0000' , AccountId = OpportunityEscalationtest.a.Id); OpportunityEscalationtest.c.add(newC); newC = new Contact( FirstName = 'Samantha' , LastName = 'Carter' , Phone = '(00) 0000 0000' , AccountId = OpportunityEscalationtest.a.Id); OpportunityEscalationtest.c.add(newC); newC = new Contact( FirstName = 'Daniel' , LastName = 'Jackson' , Phone = '(00) 0000 0000' , AccountId = OpportunityEscalationtest.a.Id); OpportunityEscalationtest.c.add(newC); insert OpportunityEscalationtest.c; Contact priCont = [Select Id from Contact where FirstName = 'Jack' limit 1]; OpportunityEscalationtest.a.GillForce__Primary_Contact__c = priCont.Id; OpportunityEscalationtest.o = new Opportunity( Name = 'Mountain Complex Water' , CloseDate = system.today() , StageName = 'Business Analysis' , AccountId = OpportunityEscalationtest.a.Id); insert OpportunityEscalationtest.o; for (Contact cont : c) { OpportunityContactRole role = new OpportunityContactRole( ContactId = cont.Id , OpportunityId = OpportunityEscalationtest.o.Id , Role = 'Descision Maker'); role.IsPrimary = (OpportunityEscalationtest.a.GillForce__Primary_Contact__c == cont.Id); insert role; } } </code></pre> <p>The error is thrown somewhere between these 2 lines of code:</p> <pre><code> insert OpportunityEscalationtest.c; Contact priCont = [Select Id from Contact where FirstName = 'Jack' limit 1]; </code></pre> <p>I'm a bit stumped, unless i am mistaken this piece of code should be self contained. Any ideas would be excellent.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload