简体   繁体   English

具有空指针异常的Salesforce Apex测试类

[英]Salesforce Apex test class with null pointer exceptions

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. 我一直在最奇怪的地方收到空指针异常。

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

The error is thrown somewhere between these 2 lines of code: 错误在以下两行代码之间抛出:

    insert OpportunityEscalationtest.c;

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

I'm a bit stumped, unless i am mistaken this piece of code should be self contained. 我有些困惑,除非我误会了这段代码应该是独立的。 Any ideas would be excellent. 任何想法都很好。

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

You are getting error on line 你得到的行错误

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

It seams that insert fails (maybe due to some conditions in triggers). 它表明插入失败(可能是由于触发器中的某些条件)。 Or it may just doesn't insert one particular Contact with name Jack (maybe because of Last name). 或者它可能只是不插入名称为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. 之后,您将了解问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM