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

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

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

Checked in debug log -

16:29:10.069 (69415926)|USER_DEBUG|[92]|DEBUG|^^^^^^^^ Entered()
16:29:10.069 (69672249)|SOQL_EXECUTE_BEGIN|[95]|Aggregations:0|SELECT Id, Market__c FROM Custom_Package__c 
16:29:10.069 (69855248)|EXCEPTION_THROWN|[95]|System.NullPointerException: Attempt to de-reference a null object
16:29:10.070 (70019991)|SYSTEM_MODE_EXIT|false
16:29:10.070 (70105646)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

External entry point

Below is the vf page and apex class -

Apex Class -

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();
                    AllName = getMarket();
                    Alldept = getDept();


            public void addRow() {
                    isRerender = true;

                   CustomPackageWrapper cw = new CustomPackageWrapper(new Custom_Package__c());


        public List<ID> discountScheduleID {
            get {
                if (discountScheduleID == null) {
                    discountScheduleID = new List<ID>();
                return discountScheduleID ;

          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 ] )
                        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>();
cp.discountSchedule = new List<ID>();
             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;

            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;

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

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

        function renderChosen() {



        function focusChosen() {

             *  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() {
            var type = $j('[id$="type"]').val();
            if (type == 'RFP') {
                $j.each(rfpRequiredIds, function(index, value) {
                    var selector = '[id$="' + value + '"]';
            } else if (type == 'OPT') {
                $j.each(optRequiredFieldIds, function(index, value) {
                    var selector = '[id$="' + value + '"]';
            } else if (type == 'SEM') {
                $j.each(semRequiredFieldIds, function(index, value) {
                    var selector = '[id$="' + value + '"]';

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

        $j(document).ready(function() {

        /* ]]> */        
       .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;

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

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

          <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 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:variable var="rowNumber" value="{!rowNumber+1}"/> 

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


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



enter image description here

  • 2
    What line is line 95? – Eric Nov 7 '15 at 11:06
  • 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 )); } – hellraizer Nov 7 '15 at 11:07
  • The for loop is line 95..in getMarket() method – hellraizer Nov 7 '15 at 11:07

You are using an uninitialized property discountSchedule in your for loop at line 95:

for(Custom_Package__c Proj1  :[select Id,Market__c from Custom_Package__c where ID=:cp.discountSchedule ] )

You are defining the property in your CustomPackageWrapper object, but I think you are never initializing it so it remains null when your code enters the for loop. I managed to reproduce the same error with similar code.


I'm not 100% sure what you are trying to achieve with your code here, and I'm not sure what the Custom_Package__c object is and is supposed to do, I can only assume.

So, I'm assuming that you want to hold a list of Custom_Package__c IDs in the discountSchedule variable based on your for loop query. The [select Id,Market__c from Custom_Package__c where ID=:cp.discountSchedule ] query means that you want to return a list of Custom_Package__c objects who's ID is contained inside the cp.discountSchedule list - that's the where ID=:cp.discountSchedule part.

IF my assumption is right, you'll first have to rewrite the WHERE part of the query so the full query is like below (as this is how you check if ID is part of a list in query):

[SELECT Id,Market__c FROM Custom_Package__c WHERE Id IN :cp.discountSchedule]

And before your for loop you'll have to obtain a list of IDs of your Custom_Package__c objects. I'm not sure how you will do that and from where, that is something that you'll have to figure out as you are the one who knows what he is trying to achieve with this VF page.

That's all I can say about this at this time, and these are all assumptions. By giving you the cause of your exception I should have given you enough to start at least. The bottom line is that your cp.discountSchedule list is null, so you need to initialize it, probably by querying for the required list of Custom_Package__c IDs.

  • Hi smukov.. Thanks for the reply .. How to initialize discountSchedule ..Should it be done inside for loop or outside.. – hellraizer Nov 7 '15 at 16:58
  • Hi smukov.. i have initialized it like this before for loop....cp.discountSchedule = new List<String>()... Still the soql query is returning nothing...In the debug i can see 0 rows returned.. Can u please check... – hellraizer Nov 7 '15 at 17:02
  • @Sid, please check my edit. Bare in mind that these are all assumptions of what you are trying to achieve. – smukov Nov 7 '15 at 17:34
  • Hi smukov...I have posted a pic about what i am trying to achieve..Currently what is not working is whenever i am selecting a value from Custom Package column, market column is not populating any data.. – hellraizer Nov 7 '15 at 17:39
  • getDept().. is the method which is populating all the Custom Packages names and getMarket() is the method which populates all the market related to Custom Package selected...And I have initialized cp.discountSchedule... – hellraizer Nov 7 '15 at 17:43

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.