0

我正在尝试编写一种测试方法,该方法需要我生成一个机会以及与该机会相关的所有记录。我不断在最奇怪的地方收到空指针异常。

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];

我有点难过,除非我弄错了,这段代码应该是自包含的。任何想法都会很棒。

4

2 回答 2

0

首先,我会尝试将您的测试类的 API 版本设置为 22.0(您的类主体的版本设置选项卡)并尝试再次运行测试。

于 2012-08-28T06:59:21.867 回答
0

您在线上遇到错误

Contact priCont = [Select Id from Contact where FirstName = 'Jack' limit 1];

它接缝插入失败(可能是由于触发器中的某些条件)。或者它可能只是没有插入一个名为 Jack 的特定联系人(可能是因为姓氏)。

What would I do if I were you. I would retrieve all Contacts and print them to the debug (or where it's easier for you). After this you will understand where is a problem.

于 2012-08-28T20:40:49.153 回答