我正在尝试编写一种测试方法,该方法需要我生成一个机会以及与该机会相关的所有记录。我不断在最奇怪的地方收到空指针异常。
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<Contact>();
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;
}
}
错误发生在这两行代码之间:
insert OpportunityEscalationtest.c;
Contact priCont = [Select Id from Contact where FirstName = 'Jack' limit 1];
我有点难过,除非我弄错了,这段代码应该是自包含的。任何想法都会很棒。