0

I'm trying to develop a VF Page that displays record with attachment. But I get the error on the controller of the page which says : Attempt to de-reference a null object Error.

Here is the code of VF Page:

    <apex:page showHeader="false"  controller="Controller004"  sidebar="false" >
<apex:messages />
<apex:pageBlock >
<apex:pageBlockSection title="Record of avalaible Doctors" >
<apex:form >
<apex:pageBlockTable value="{!wc}" var="a" columns="4" cellpadding="15px">

 <apex:column headerValue="Name">
 <apex:outputField value="{!a.doctor.Full_Name__c}"  />
 </apex:column>
 <apex:column headerValue="Fee">
<apex:outputField value="{!a.doctor.Fee__c}"/>
</apex:column>
<apex:column headerValue="Schedule">
<apex:outputField value="{!a.doctor.schedule__c}"/>
</apex:column>
<apex:column headerValue="Photo" >
<apex:image url="{!a.url}" height="300px" width="200px" />
</apex:column>
</apex:pageBlockTable>
</apex:form>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>

And the Controller of the page:

    global class Controller004{   
 public List<Doctor__c> obj {get;set;}
 public List<WrapperClass> wc{get;set;}

 public Controller004(){

 obj=[select Full_Name__c, Fee__c, schedule__c from Doctor__c];
 Map<Id,Id> mapDocToAtt = new Map<Id,Id>();
 for(Attachment a : [select id from Attachment where ParentId in:obj])
 { 
    mapDocToAtt.put(a.ParentId, a.Id);
 }

 for(Doctor__c doctor : [select Full_Name__c, Fee__c, schedule__c from Doctor__c])
 {

    wc.add(new WrapperClass(doctor, '/servlet/servlet.FileDownload?file='+mapDocToAtt.get(doctor.Id)));

 }
}

 public class WrapperClass{
 public Doctor__c doctor{get;set;}
 public String url{get;set;}
 public WrapperClass(Doctor__c doctor, String url){
 this.doctor=doctor;
 this.url=url;

 }



 }


}

1 Answer 1

3

before adding elements to the list, you have to initialize the list..

wc = new List<WrapperClass>();

and then you can add the elements to the list in your for loop..

for(Doctor__c doctor : [select Full_Name__c, Fee__c, schedule__c from Doctor__c])
{
    wc.add(new WrapperClass(doctor, '/servlet/servlet.FileDownload?file='+mapDocToAtt.get(doctor.Id)));
}
1
  • 1
    @Nishant Make sure you mark the answer as the correct one so future visitors can know it's right, and also so we don't waste our time trying to answer a question that already has an answer.
    – sfdcfox
    Apr 11, 2016 at 5:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.