我正在尝试编写一种测试方法,要求我生成机会以及与机会相关的所有记录。我一直在最奇怪的地方得到空指针异常。
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];
我有点难过,除非我弄错了这段代码应该是自包含的。任何想法都会很棒。
答案 0 :(得分:0)
首先,我会尝试将您的测试类的API版本设置为22.0(类体的版本Sttings选项卡)并尝试再次测试。
答案 1 :(得分:0)
你在线上遇到错误
Contact priCont = [Select Id from Contact where FirstName = 'Jack' limit 1];
接缝失败的接缝(可能是由于触发器中的某些条件)。或者它可能只是没有插入一个名字杰克的特定联系人(也许是因为姓氏)。
如果我是你,我该怎么办?我将检索所有联系人并将其打印到调试(或者对您来说更容易)。在此之后,您将了解问题所在。