Skip to main content
Hey guys,

I am trying to get the related list from a related object on a visualforce page, the flow of the object relationship is:

Account> Subscription>Subscriptions Items

I'd like to display a related list of Subscription Items on the Account page that meet my soql query. When I add the page to the layout I get "Attempt to de-reference a null object"

Below is my code:

VF Page

<apex:page standardController="Account" showHeader="true" extensions="TestDisplayQueryResults">

<!-- Define Tab panel .css styles -->

<style>

.activeTab {background-color: ⌗236FBD; color:white; background-image:none}

.inactiveTab { background-color: lightgrey; color:black; background-image:none}

</style>

<!-- Create Tab panel -->

<apex:tabPanel switchType="client" selectedTab="name2" id="AccountTabPanel"

tabClass="activeTab" inactiveTabClass="inactiveTab">

<apex:tab label="Subscriptions" name="name1" id="tabOne">

<apex:relatedList list="Subscriptions__r"/>

</apex:tab>

<apex:tab label="PCS" name="name2" id="tabTwo">

<apex:pageBlock title="My Content">

<apex:pageBlockTable value="{!SubItems}" var="item">

<apex:column value="{!item.name}"/>

<apex:column value="{!item.id}"/>

</apex:pageBlockTable>

</apex:pageBlock>

</apex:tab>

</apex:tabPanel>

</apex:page>

Extension:

 

public with sharing class TestDisplayQueryResults

{

public String currentRecordId {get;set;}

public Account acc{get;set;}

public List<Subscription__c> Subs{get; set;}

public List<Subscription_Item__c> SubItems{get; set;}

public TestDisplayQueryResults(ApexPages.StandardController controller)

{

currentRecordId = ApexPages.CurrentPage().getparameters().get('id');

acc = [select id ,name from Account where id =: currentRecordId ];

Subs = [select id, Name, (Select id, name, Product_Code_PCS_TSM__c from Subscription_Items__r where Product_Code_PCS_TSM__c = true) from Subscription__c where Account_ID__c = :acc.Id];

for(Subscription__c Subs : [select id, Name, (Select id, name, Product_Code_PCS_TSM__c from Subscription_Items__r where Product_Code_PCS_TSM__c = true) from Subscription__c where Account_ID__c= :acc.Id])

{

for(Subscription_Item__c items: Subs.Subscription_Items__r)

{

SubItems.add(items);

}

}

}

}

 
4 answers
  1. Feb 28, 2019, 8:17 PM
    try this

     

    public with sharing class TestDisplayQueryResults

    {

    public String currentRecordId {get;set;}

    public Account acc{get;set;}

    public List<Subscription__c> Subs{get; set;}

    public List<Subscription_Item__c> SubItems{get; set;}

    public TestDisplayQueryResults(ApexPages.StandardController controller)

    {

    currentRecordId = ApexPages.CurrentPage().getparameters().get('id');

    SubItems = new List<Subscription_Item__c>();

    acc = [select id ,name from Account where id =: currentRecordId ];

    Subs = [select id, Name, (Select id, name, Product_Code_PCS_TSM__c from Subscription_Items__r where Product_Code_PCS_TSM__c = true) from Subscription__c where Account_ID__c = :acc.Id];

    for(Subscription__c Subs : [select id, Name, (Select id, name, Product_Code_PCS_TSM__c from Subscription_Items__r where Product_Code_PCS_TSM__c = true) from Subscription__c where Account_ID__c= :acc.Id])

    {

    for(Subscription_Item__c items: Subs.Subscription_Items__r)

    {

    SubItems.add(items);

    }

    }

    }

    }

     
0/9000