3

I have written the following code to upsert the selected records using Wrapper classes. But I am getting Null Pointer exception . I am not understanding what I have missed. Please suggest. How can I save the records.

    public class DynamicRows
    {
        public List<Product__c> ProductNameList{get;set;}
        public Order__c micOrder{get; set;}
        public String item{get; set;}
        public List<wrapProduct> wrapProductList{get;set;}
        public Boolean selected{get; set;}
        public List<OrderProduct__c> selectedProduct{get;set;}

         public DynamicRows(ApexPages.StandardController controller)
         {
            micOrder = (Order__c)controller.getRecord();
            System.debug('Order Id ' + micOrder.id);
            ProductNameList = new List<Product__c>();
            ProductNameList = [Select Name, Quantity__c, Price__c, Packaging__c from Product__c];
         }

          public void productsSelected() {
            selectedProduct= new List<MiC_OrderProduct__c>();
      wrapProductList = new  List<wrapProduct>();
            for(wrapProduct wrapProductObj : wrapProductList) {
                if(wrapProductObj.selected == true) {
                    System.debug( 'show ' + wrapProductObj.OrderProd.Product__c);
                    selectedProduct.add(wrapProductObj.OrderProd);
                }
            }

            if(!selectedProduct.isEmpty()){
                upsert selectedProduct;
            }

           ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Info,'Record Saved Successfully.');
           ApexPages.addMessage(myMsg);

        }
         public void checkAll()
        {

          //  for(wrapProduct wrapProductObj : wrapProductList) {
          //      wrapProductObj.selected = true;
               // isrendered = false;
          //  system.debug('exited checkAll');
        }

        public class wrapProduct{
            public OrderProduct__c OrderProd {get; set;}
            public Boolean selected {get; set;}

            public wrapProduct(Product__c prodObj) {
                OrderProd.Product__c = prodObj.Id;
                selected = false;
            }
        }
    }

Page:

<apex:page standardController="Order__c" extensions="DynamicRows"> 
   <apex:form >
      <apex:pageBlock >  
                <apex:pageBlockButtons >
                    <apex:commandButton value="Save Products Selected" action="{!productsSelected}" rerender="table"/>
                </apex:pageBlockButtons>
                <apex:pageBlockTable value="{!ProductNameList}" var="prod">
                 <apex:pageMessages ></apex:pageMessages>
                    <apex:column headerValue="Product">
                        <apex:outputField value="{!prod.Name}"/>
                    </apex:column>
                    <apex:column headerValue="Quantity">
                        <apex:inputField value="{!prod.Quantity__c}"/>
                    </apex:column>    
                    <apex:column headerValue="Price">
                        <apex:inputField value="{!prod.Price__c}"/>
                    </apex:column> 
                    <apex:column headerValue="Packaging">
                        <apex:inputField value="{!prod.Packaging__c}"/>
                    </apex:column>  
              <!--      <apex:column headerValue="Select Product">
                        <apex:inputCheckbox value="{!selected}" id="checkdone"/>
                    </apex:column>  -->
                     <apex:column >
                         <apex:facet name="header">
                              <apex:inputCheckbox onclick="checkAllBoxes(this)"> </apex:inputCheckbox>
                         </apex:facet>
                      <apex:inputCheckbox value="{!selected}" id="checkdone"/> 
                    </apex:column>                      
               </apex:pageBlockTable>
                <apex:actionFunction name="callCheckAllBoxes" action="{!checkAll}" reRender="selected"/>

       </apex:pageBlock>
    </apex:form>
</apex:page>
  • If this is being reported in your Visualforce page, you can get the line number involved most simply by turning on "Development Mode" for the User that you are testing with. That is important information to help you figure out the problem and then if you can't to include in a question like this. – Keith C Jul 24 '14 at 17:42
  • wrapProductList isn't referenced anywhere on your page and would appear to be just an empty list and the reason why your upsert is failing – greenstork Jul 24 '14 at 17:44
  • I am getting error in for(wrapProduct wrapProductObj : wrapProductList) { if(wrapProductObj.selected == true) { System.debug( 'show ' + wrapProductObj.OrderProd.Product__c); selectedProduct.add(wrapProductObj.OrderProd); } } – user2703132 Jul 24 '14 at 17:44
  • Can u pls suggest how can I accept user input here – user2703132 Jul 24 '14 at 17:45
  • I have added wrapProductList = new List<wrapProduct>(); to the code but I am still not able to save the records. though I am not getting any error now – user2703132 Jul 24 '14 at 17:49
1

The code you have posted is a little confusing. Here is the basic structure of how to present a list of objects and to update the selected ones:

public class DynamicRows {

    public List<wrapProduct> wrapProductList{get;set;}

    public DynamicRows(ApexPages.StandardController controller) {
        wrapProductList = new List<wrapProduct>();
        for (Product__c p : [Select Name, Quantity__c, Price__c, Packaging__c from Product__c]) {
            wrapProductList.add(new wrapProduct(p));
        }
    }

    public void productsSelected() {
        List<Product__c> selectedProduct = new List<Product__c>();
        for (wrapProduct wrapProductObj : wrapProductList) {
            if (wrapProductObj.selected) {
                selectedProduct.add(wrapProductObj.OrderProd);
            }
        }
        update selectedProduct;
    }

    public class wrapProduct{
        public OrderProduct__c OrderProd {get; set;}
        public Boolean selected {get; set;}
        public wrapProduct(Product__c prodObj) {
            OrderProd.Product__c = prodObj.Id;
            selected = false;
        }
    }
}

Use this as a starting point. You can modify productsSelected if you want to update some other object (e.g. MiC_OrderProduct__c).

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.