+ Start a Discussion
❤Code❤Code 

wrapper class - System.NullPointerException: Attempt to de-reference a null object

I have a vf page where i am dynamically adding rows. I am using wrapper class to add empty rows evrytime.

On Vf page there is an action support which calls getMarket() method. The prob here is i am unable to figure out how to use discountSchedule (present in wrapper class) variable in getMarket() SOQL so that my action support will work.

Now after clicking addrow button i am getting the following error - 

 
System.NullPointerException: Attempt to de-reference a null object 
External entry point




Below is the vf page and apex class -

Apex Classs - 

    public with sharing class RFPController{

        public RFP__c accounts;
            public Custom_Package__c del;
            public List < Custom_Package__c > addattendeeList {get;set;}
            public List < Custom_Package__c > delattendeeList {get;set;}
            public List < Custom_Package__c > attendeeList {get;set;}
            public Integer totalCount {get;set;}
            
            public Integer rowIndex {get;set;}
            public Integer deleterowIndex {get;set;}
            public List < Custom_Package__c > delAttendees {get;set;}
            public Boolean isRerender{get;set;}
            //public String S {get;set;}
             public Integer numberOfRowToRemove { get; set; }
             public String selectedRowIndex{get;set;} 
            //public Integer count = 1;
             public Integer c{get; set;} 
             //public Integer counter{get;set;}
             private List<SelectOption> options;
             public List<SelectOption> AllName{get;set;}
             public List<SelectOption> AllDept{get;set;}
            // public String discountScheduleID { get; set; }
            public  string mark {get; set;}
            
            Public List<CustomPackageWrapper> cplist = new List<CustomPackageWrapper>();
            public CustomPackageWrapper cp = new CustomPackageWrapper (new Custom_Package__c() ); 
            
            private List<Custom_Package__c>  acc {get;set;}
            
            public RFPController(ApexPages.StandardController controller) {

                    isRerender = false;
                    accounts =  (RFP__c )controller.getRecord();
                    String S = 'Select id, Name, Market__c, State__c, First_Issue__c,Last_Issue__c,Circulation__c,Space_Unit__c,Per_Unit_Open_Rate__c,Space_Discount__c from Custom_Package__c ';
                    //totalCount = attendeeList.size();
                    attendeeList  = Database.Query(S);
                    attendeeList = new List < Custom_Package__c >();
                    for(integer i=0;i<1;i++){ 
                    attendeeList .add(new Custom_Package__c ());
                    }
                    delattendeeList = new List < Custom_Package__c > ();
                    delattendees = new List < Custom_Package__c > ();
                   
                    accounts.Rep_Name__c= Userinfo.getuserId();
                    c=0;
                    AllName = getMarket();
                    Alldept = getDept();
                    
            }
     
            public void addRow() {
                    isRerender = true;
                    
                   CustomPackageWrapper cw = new CustomPackageWrapper(new Custom_Package__c());
                   attendeeList1.add(cw);


            }

            

        public List<ID> discountScheduleID {
            get {
                if (discountScheduleID == null) {
                    discountScheduleID = new List<ID>();
                }
                return discountScheduleID ;
            }
            set;
        }
        
          public List<SelectOption> getDept()
        {
            List<SelectOption> options = new List<SelectOption>();
            //options.add(new SelectOption('Select','--Select--'));
            
            for(Custom_Package__c Proj  :[select Id, Name from Custom_Package__c ] )
                 {
                        system.debug('3333333'); 
                        options.add(new SelectOption(Proj.ID,Proj.Name));
                        
                 }
            return options;
            
        }
     
       
       public Integer selectedRowIdx {get; set;}

        
        public List<SelectOption> getMarket(){
            system.debug('^^^^^^^^ Entered' + discountScheduleID );
                List<SelectOption> options = new List<SelectOption>();

             for(Custom_Package__c Proj1  :[select Id,Market__c from Custom_Package__c where ID=:cp.discountSchedule ] )
                    {
                        system.debug('3333333' + discountScheduleID ); 
                        CustomPackageWrapper cp = new CustomPackageWrapper (new Custom_Package__c());
                         cp.discountSchedule.add(Proj1.Market__c );
                         cplist.add(cp );
                        options.add(new SelectOption(Proj1.ID,Proj1.Market__c ));
                        
                    }
                  
            system.debug('^^^^^^^^ Entered' + options.size());        
            return options;
            

        }
        
        public string selectedMarketIds {get; set;}  
        
            public void FindAllName()
        {
            AllName= new List<SelectOption>();
            AllDept= new List<SelectOption>();
            system.debug('!!!!!!! Entered');
            AllName = getMarket();
            Alldept = getDept();
            system.debug('&&&&&&& Entered' + AllName );

        }
        
        public List<CustomPackageWrapper> attendeeList1 {
            get {if (attendeeList1 == null)
                 attendeeList1 = new List<CustomPackageWrapper>();
                 return attendeeList1;
           }
            set;
            }



            public class CustomPackageWrapper {
            public Custom_Package__c cp {get; set;}
            public List<String> discountSchedule {get; set;}
            public Integer rowIndex {get;set;}
            
            public CustomPackageWrapper(Custom_Package__c cp) {this.cp = cp;}
            
           
            }

      
    }

VF Page - 

   <apex:page standardController="RFP__c" extensions="RFPController" sidebar="false">

<meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <c:FixSfdcFocusHack />
    <c:JQuery />
    <c:Chosen />
    <style type="text/css">
        .fullWidth { width: 100%; }
        .flightDateRangeText { font-size: 90%;  }

        .bPageBlock .requiredForSubmit .requiredBlock {
        background-color: #FFD700;
        bottom: 1px;
        left: -4px;
        position: absolute;
        top: 1px;
        width: 3px;
        }

        .bPageBlock .requiredForSubmitInput {
        height: 100%;
        position: relative;
        }

    </style>
    <script type="text/javascript">
    /* <![CDATA[ */

    /* 
         *  Render Chosen on select elements with the chzn-select class specified.
         */

    function renderChosen() {

            $j('.chzn-select').chosen();

    } 

    function focusChosen() {
        $j('.search-field').find("input").focus();       
    }       

    /*
         *  Render required marker on elements where class == "required".
         */
    function renderRequired() {
        $j('.required').wrap('<div class="requiredInput" />').before('<div class="requiredBlock" />');
    }

    /*
         *  Render required marker on elements with the specified ID.
         */        
    function renderRequiredComponentId(componentId) {
        $j(jq(componentId) + ' .required').wrap('<div class="requiredInput" />').before('<div class="requiredBlock" />');
    }

    var rfpRequiredIds = ["dueDate", "clientsEstablishedPartners", "campaignMarketingObjectives", "campaignGoals",
                          "scopeOfCampaign", "mediaRequested", "otherMediaRequested", "campaignTotalNet", 
                          "primaryTargetAudience", "geographies", "servingPreference", "clientServedSpecifics", "adSizes",
                          "creativeType", "richMediaType", "percentageFlash", "percentageRichMedia", "flightDateRanges"];

    var optRequiredFieldIds = ["planCompletionDate", "clientsEstablishedPartners",
                               "campaignMarketingObjectives", "campaignGoals", "scopeOfCampaign", "mediaRequested", "campaignComments",
                               "primaryTargetAudience", "geographies", "customGeoDetail", "servingPreference", "clientServedSpecifics",
                               "adSizes", "creativeType", "richMediaType", "additionalCreativeNotes"];

    //SSirikonda - 07/25/2013 - Removing the field Plan_completion_Date__c as yellow marked field when
    //RFP type drop-down = SEM   
    /*var semRequiredFieldIds = ["planCompletionDate", "url", 
           "campaignMarketingObjectives", "campaignComments", "geographies", "customGeoDetail"];*/

    var semRequiredFieldIds = ["url", 
                               "campaignMarketingObjectives", "campaignComments", "geographies", "customGeoDetail"];

    function modifyRequired() {
        $j('.requiredForSubmitInput').removeClass('requiredForSubmitInput');
        var type = $j('[id$="type"]').val();
        if (type == 'RFP') {
            $j.each(rfpRequiredIds, function(index, value) {
                var selector = '[id$="' + value + '"]';
                $j(selector).addClass('requiredForSubmitInput');
            });
        } else if (type == 'OPT') {
            $j.each(optRequiredFieldIds, function(index, value) {
                var selector = '[id$="' + value + '"]';
                $j(selector).addClass('requiredForSubmitInput');
            });
        } else if (type == 'SEM') {
            $j.each(semRequiredFieldIds, function(index, value) {
                var selector = '[id$="' + value + '"]';
                $j(selector).addClass('requiredForSubmitInput');
            });
        }
    }

    function changePrimaryContact() {
        var value = $j('[id$="primaryContact"]').val();
        var selector = '[id$="agencyContact"]';
        $j(selector).removeClass('requiredInput');
        var selector = '[id$="agency"]';
        $j(selector).removeClass('requiredInput');
        if (value == 'Agency') {
            var selector = '[id$="agency"]';
            $j(selector).addClass('requiredInput');
            var selector = '[id$="agencyContact"]';
            $j(selector).addClass('requiredInput');
        }
    }

    $j(document).ready(function() {
        renderChosen();
        renderRequired();
        clearErrors();
        changePrimaryContact();
        modifyRequired();
    });

    /* ]]> */        
    </script>
    <style>
   .twitterStyleTextbox {
    border: 1px solid #c4c4c4;
    width: 180px;
    height: 18px;
    font-size: 13px;
    padding: 4px 4px 4px 4px;
    border-radius: 4px;
    -moz-border-radius: 4px;
    -webkit-border-radius: 4px;
    box-shadow: 0px 0px 8px #d9d9d9;
    -moz-box-shadow: 0px 0px 8px #d9d9d9;
    -webkit-box-shadow: 0px 0px 8px #d9d9d9;
}

.twitterStyleTextbox:focus {
    outline: none;
    border: 1px solid #7bc1f7;
    box-shadow: 0px 0px 8px #7bc1f7;
    -moz-box-shadow: 0px 0px 8px #7bc1f7;
    -webkit-box-shadow: 0px 0px 8px #7bc1f7;
}
 </style>


<apex:form id="myForm">
<apex:pageBlock id="pb">
        <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save"/>
                <apex:commandButton action="{!Cancel}" value="Cancel"/>
         </apex:pageBlockButtons>

<apex:pageMessages />
      <apex:outputText rendered="{!isRerender}">

      </apex:outputText>      
      <apex:variable var="rowNumber" value="{!0}"/>


      <apex:pageBlockSection title="Child RFP Details" collapsible="false" id="section">

        <apex:pageBlockTable id="thetable"  var="acc" value="{!attendeeList1}" onRowMouseOver="removeHighlight(this)">

             <apex:column headerValue="Action" >
            <apex:commandLink value="Copy" reRender="pb" onclick="testMe(event)"/>
          </apex:column>

          <apex:column headerValue="Custom Package">
                 <apex:param name="rowIndex" value="{!rowNumber}"/>
                         <apex:selectList multiselect="true" value="{!acc.discountSchedule}"  styleClass="fullWidth chzn-select" size="1" style="width:400px;" >
                          <apex:actionSupport event="onchange" action="{!FindAllName}" rerender="geographies"  oncomplete="renderChosen()"/>
                        <apex:selectOptions value="{!Dept}"/>
                      </apex:selectList> 
                <apex:variable var="rowNumber" value="{!rowNumber+1}"/> 
          </apex:column>


          <apex:column headerValue="Market" id="market">
            <apex:outputPanel id="geographies" layout="block" >
          <apex:selectList id="selectedmarkets" multiselect="false" value="{!selectedMarketIds }"  styleClass="fullWidth chzn-select" size="1" style="width:150px" >

              <apex:selectOptions value="{!Market}"/>
             </apex:selectList> 
            </apex:outputPanel>
          </apex:column>

          <apex:column headerValue="State" >
            <apex:inputField value="{!acc.cp.State__c}" styleClass="twitterStyleTextbox"/>
          </apex:column>
          <apex:column headerValue="First Issue" >
            <apex:inputField value="{!acc.cp.First_Issue__c}" styleClass="twitterStyleTextbox"/>
          </apex:column>
          <apex:column headerValue="Last Issue" >
            <apex:inputField value="{!acc.cp.Last_Issue__c}" styleClass="twitterStyleTextbox"/>
          </apex:column>
          <apex:column headerValue="Circulation" >
            <apex:inputField value="{!acc.cp.Circulation__c}" styleClass="twitterStyleTextbox"/>
          </apex:column>
            <apex:column headerValue="Space Unit">
            <apex:inputField value="{!acc.cp.Space_Unit__c }" styleClass="chzn-select"/>
          </apex:column>
          <apex:column headerValue="Space Discount">
          <apex:inputField value="{!acc.cp.Space_Discount__c}" styleClass="twitterStyleTextbox"/>
          </apex:column>
           <apex:column headerValue="Per Unit Open Rate">
          <apex:inputField value="{!acc.cp.Per_Unit_Open_Rate__c}" styleClass="twitterStyleTextbox"/>
          </apex:column>

        </apex:pageBlockTable>

      </apex:pageBlockSection>
      <apex:commandButton action="{!addRow}" value="Add Row" reRender="pb" oncomplete="renderChosen()"> 
      <apex:param name="rowIndex" value="{!rowNumber}"/>

       </apex:commandButton>

</apex:pageblock>
</apex:form>
</apex:page>                

Regards
 
 
Shashi PatowaryShashi Patowary
Hi,

My first observation is - try to initialize 'attendeeList1'  list in the constructor itself (inside 'RFPController').
Please let me know if this is helpful.

Regards,
Shashi Patowary
 
❤Code❤Code
Hi Shashi,

I am still gettng the error - 

System.NullPointerException: Attempt to de-reference a null object 
External entry point

And it is happening after line no 94..


15:53:02.077 (77849173)|SOQL_EXECUTE_BEGIN|[94]|Aggregations:0|SELECT Id, Market__c FROM Custom_Package__c 15:53:02.078 (78032313)|EXCEPTION_THROWN|[94]|System.NullPointerException: Attempt to de-reference a null object 15:53:02.078 (78196818)|SYSTEM_MODE_EXIT|false 15:53:02.078 (78281860)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object


Regards
Shashi PatowaryShashi Patowary
Hi,

Is this the line no. 94 -
             for(Custom_Package__c Proj1  :[select Id,Market__c from Custom_Package__c where ID=:cp.discountSchedule ] )

Here you are trying to search Custom_Package__c object records with discountSchedule variable of  CustomPackageWrapper class.
discountSchedule variable is never set in the wraper class instantiation.I think this is creating issue.


 
public class CustomPackageWrapper {
            public Custom_Package__c cp {get; set;}
            public List<String> discountSchedule {get; set;}
            public Integer rowIndex {get;set;}
            
            public CustomPackageWrapper(Custom_Package__c cp) {this.cp = cp;}
            
           
            }
Please let me know if this is helpful.

regards,
Shashi Patowary