Visualforce filtered related list error: Content cannot be displayed: Attempt to de-reference a null object

Visualforce filtered related list error: Content cannot be displayed: Attempt to de-reference a null object

我正在尝试显示机会的合同列表。该列表应包括与商机上的帐户或其父帐户关联的任何合同。尽管测试 SOQL return 2 条合同记录,但我收到错误:

Content cannot be displayed: Attempt to de-reference a null object

这是 visualforce 页面代码:

<apex:page standardController="Opportunity" extensions="RelatedContracts" tabStyle="Opportunity">
    <apex:form>
        <apex:pageBlock title="Contracts List" id="ContractId">

            <apex:pageBlockTable value="{!contracts}" var="c">
                <apex:column headerValue="ContractName">
                    <apex:outputLink value="{!URLFOR('/')}{!c.Id}" target="_blank">{!c.ContractNumber}
                    </apex:outputLink>
                </apex:column>
                <apex:column value="{!c.Status}" />
                <apex:column value="{!c.StartDate}" />
                <apex:column value="{!c.Enddate}" />
                <apex:column value="{!c.SpecialTerms}" />
                <apex:column value="{!c.Description}" />

            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>

</apex:page>

这里是分机 class:

public with sharing class RelatedContracts {

    public Opportunity opp {
        get;
        set;
    }
    public List < Contract > contracts {
        get;
        set;
    }

    public RelatedContracts(ApexPages.StandardController controller) {
        opp = (Opportunity) controller.getRecord();

    }

    Opportunity oppInfo = [SELECT AccountId from Opportunity WHERE id = : opp.Id];
    Account acct = [SELECT Id, ParentId from Account WHERE id = : oppInfo.AccountId];

    public List < Contract > getContracts() {
        contracts = new List < Contract > ();
        contracts = [SELECT Id, RecordTypeId, Status, ContractNumber, StartDate, EndDate, SpecialTerms, Description FROM Contract
            WHERE AccountId = : opp.AccountId OR AccountId = : acct.ParentId
        ];
        System.debug('ContractList' + contracts);
        if (contracts.size() > 0) {
            return contracts;
        } else
            return null;

    }
}

请尝试在 class 中定义 oppInfo 和 acct 和 null。在构造函数中使用 soql 设置值。 在 getContracts() 中使用 oppInfo 而不是 opp 变量。