function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TylerBrooksTylerBrooks 

Attempt to De-reference null object

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


 
Best Answer chosen by TylerBrooks
Raj VakatiRaj Vakati
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);
            }
        }
    }
}

 

All Answers

Raj VakatiRaj Vakati
On whihc line you are getting 
TylerBrooksTylerBrooks
Raj this is the exception email I'm getting: 
caused by: System.NullPointerException: Attempt to de-reference a null object
 
Class.TestDisplayQueryResults.<init>: line 16, column 1
 
Raj VakatiRaj Vakati
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);
            }
        }
    }
}

 
This was selected as the best answer
TylerBrooksTylerBrooks
I had to remove the <b> and </b> but it worked great. I just forgot to initialize the list. Thank you so much raj