-3

I am creating a vf page where a user can create multiple orderitem for a order by adding dynamic rows.

When a user selects any product from product lookup, i have an actionsupport which will query product2 and populate values like productcode, unit price.

But after selecting the product i am getting Attempt to de-reference a null object

 **Getting errr at this line**          wrapper.odi.Product2 = [Select ProductCode,Name ,Family ,description ,(Select UnitPrice, Product2Id From PricebookEntries) From Product2 where Id = :wrapper.odi.Product2Id];

error

Below is the code

Apex class -

  public class CreateOrders {

        public Order order{get;set;}
        public List<orderitemclass> otlist {get;set;}
        public Pricebook2 standardBook;
        public OrderItem oti;
        public String wrapperId { get; set; }
        public String changedPosRevWrapperId { get; set; }

         public CreateOrders(ApexPages.StandardController controller){
             order = new Order();
             oti = new OrderItem();
             otlist = new List<orderitemclass>();
             otlist.add(new orderitemclass(oti));
             standardBook = [SELECT Id FROM Pricebook2 WHERE IsStandard = true];
         }
        private orderitemclass getrfpWrapper(String wrapperId) {
            for (orderitemclass rfpWrapper : otlist){
                if (rfpWrapper.wrapperId == wrapperId)

                    return rfpWrapper;
            }
            return null;
        }
        public void productdetails(){
            orderitemclass wrapper = getrfpWrapper(changedPosRevWrapperId);
  **Getting errr at this line**          wrapper.odi.Product2 = [Select ProductCode,Name ,Family ,description ,(Select UnitPrice, Product2Id From PricebookEntries) From Product2 where Id = :wrapper.odi.Product2Id];
        }
        public class orderitemclass{
            public OrderItem odi {get; set;}
            public String wrapperId { get; set; }
            public orderitemclass(OrderItem ot){
                odi = new OrderItem();
                odi = ot;
                Blob b = Crypto.GenerateAESKey(128);
                String h = EncodingUtil.ConvertTohex(b);
                wrapperId = h.SubString(0,8)+ '-' + h.SubString(8,12) + '-' + h.SubString(12,16) + '-' + h.SubString(16,20) + '-' + h.substring(20);
            }
        }
    }

Apex Page -

<apex:pageBlockTable value="{!otlist}" var="otiWrap" id="table">
                    <apex:column  headerValue="Product">
                         <apex:inputfield value="{!otiWrap.odi.Product2Id}">
                             <apex:actionSupport action="{!productdetails}" event="onchange" reRender="table"/>
                        </apex:inputfield>
                    </apex:column>
                    <apex:column  headerValue="Product Code">
                        <apex:inputfield value="{!otiWrap.odi.Product2.ProductCode}"/>
                    </apex:column>
                    <apex:column  headerValue="Quantity">
                        <apex:inputfield value="{!otiWrap.odi.Quantity}"/>
                    </apex:column>
                    <apex:column  headerValue="Unit Price">
                        <apex:inputfield value="{!otiWrap.odi.unitprice}"/>
                    </apex:column>
                    <apex:column  headerValue="Line Description">
                        <apex:inputfield value="{!otiWrap.odi.Description}"/>
                    </apex:column>
                    <apex:column  headerValue="Total Price">
                        <apex:inputfield value="{!otiWrap.odi.totalprice}"/>
                    </apex:column>
                   </apex:pageBlockTable>
  • 2
    You don’t seem to be populating Product2 in your odi variable anywhere. And that may be reason while accessing wrapper.odi.Product2, you are getting the exception. – Jayant Das Apr 7 '18 at 13:08
0

The problem was with actionsupport on vf page..i missed to pass apex:param wrapperid to the controller...This fixed the issue.

Below is the code which i have added to fix the issue -

<apex:actionSupport action="{!productdetails}" event="onchange" reRender="table">
    <apex:param name="wrapperId" value="{!otiWrap.wrapperId}" assignTo="{!changedPosRevWrapperId}" />
</apex:actionSupport>

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.