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
Micky MMicky M 

SOQL Join

Hi all this is doing my head in all i have is a query:

 

Select p.PO_Ref__c, (Select id, CreatedDate, Effort__c, Billed__c,Submitted_Date__c From Time_Sheets__r) From Project_Activity__c p

 

now i want to loop through each project activity and pull out the time sheet so ive written:

 

for(Project_Activity__c p : ProjectActivityList)
{
    for(Time_Sheet__c t : p.Time_Sheets__r)
    {   

        TimeSheetList.add(t);
    }

}

 

Am i doing something stupid here? i thought that work work fine but its complaining that im trying to de-reference a null object.

 

Does anyone have any ideas?

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
*werewolf**werewolf*

Basically your code appears to be OK, but what is TimeSheetList, and how have you declared it?  You should have a line like

 

List<Time_Sheets__c> TimeSheetList = new List<Time_Sheets__c>();

 

prior to calling this loop.

All Answers

Chamil MadusankaChamil Madusanka

Hi,

 

You are trying to refer object or object list without initialzing. get help from following links.

 

http://boards.developerforce.com/t5/Apex-Code-Development/Attempt-to-de-reference-a-null-object-Cant-figure-out-why-I-am/m-p/328099#M58155

 

http://boards.developerforce.com/t5/Visualforce-Development/System-NullPointerException/m-p/327541#M40090

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

chamil's blog

*werewolf**werewolf*

Basically your code appears to be OK, but what is TimeSheetList, and how have you declared it?  You should have a line like

 

List<Time_Sheets__c> TimeSheetList = new List<Time_Sheets__c>();

 

prior to calling this loop.

This was selected as the best answer
*werewolf**werewolf*

And I presume too that you're populating ProjectActivityList before getting into this loop:

 

List<Project_Activity__c> ProjectActivityList = [Select p.PO_Ref__c, (Select id, CreatedDate, Effort__c, Billed__c,Submitted_Date__c From Time_Sheets__r) From Project_Activity__c p];

Rahul SharmaRahul Sharma

Only Initialization doesn't work,

 

You would also need to check if the child record list is empty or not.

 

List<Project_Activity__c> ProjectActivityList = new List<Project_Activity__c>([Select p.PO_Ref__c, (Select id, CreatedDate, Effort__c, Billed__c,Submitted_Date__c From Time_Sheets__r) From Project_Activity__c p]);
if(!ProjectActivityList.isEmpty()) 
for(Project_Activity__c p : ProjectActivityList)
{
    if(!p.Time_Sheets__r.isEmpty())     
    for(Time_Sheet__c t : p.Time_Sheets__r)
    {   
        TimeSheetList.add(t);
    }
}

 

 

Micky MMicky M

Thanks all, it turn out that the code was ok i'd just not declared the lists properly so i just added :

 

List<Time_Sheet__c> TimeSheetList = new List<Time_Sheet__c>();

List<Project_Activity__c> ProjectActivityList = new List<Project_Activity__c>();

 

and it now works. cheers all.