You need to sign in to do that
Don't have an account?
manjunath vivek
Logic is not working
Hi,
Iam trying add the values from the inputfield of the sandard object product to add cart list (whenc addcart command button is clicked i need to to list out the values of product).I am getting error message.
System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!additems}' in component <apex:commandButton> in page addcart: Class.addcart.additems: line 11, column 1
Class.addcart.additems: line 11, column 1
Iam trying add the values from the inputfield of the sandard object product to add cart list (whenc addcart command button is clicked i need to to list out the values of product).I am getting error message.
<apex:page standardController="product2" showheader="false" sidebar="false" recordSetVar="Product2" extensions="addcart"> <apex:form > <apex:pageBlock > <apex:pageBlockTable value="{!product2}" var="pitem"> <apex:column headerValue="Product"> <apex:outputText value="{!pitem.name}"/> </apex:column> <apex:column headerValue="Price"> <apex:outputText value="{!pitem.Product_price__c}"/> </apex:column> <apex:column headerValue="Number of products"> <apex:inputText value="{!pitem.Number_of_products__c}"/> </apex:column> </apex:pageBlockTable> </apex:pageBlock> <apex:commandButton action="{!additems}" value="Add items"/> <apex:pageBlock title="Your items" id="shopping_cart"> <apex:outputText value="{!message}" /> </apex:pageBlock> </apex:form> </apex:page> Public class addcart { public addcart(ApexPages.StandardSetController controller) { } public String message { get; set; } public PageReference additems() { message = 'You bought: '; for (Displayproducts p: products) { if (p.count> 0) { message += p.newproduct.name ; System.debug('test'+message); } } return null; } Displayproducts[] products; public class Displayproducts { public product2 newproduct { get; set; } public Decimal count{ get; set; } public Displayproducts(product2 item) { this.newproduct = item; } } public Displayproducts[] getproducts() { if(products == null) { Products=new Displayproducts[]{}; for(Product2 item : [SELECT Id, Name,Number_of_products__c FROM product2]) { products.add(new displayproducts(item)); } } return products; } }
System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!additems}' in component <apex:commandButton> in page addcart: Class.addcart.additems: line 11, column 1
Class.addcart.additems: line 11, column 1
As you are not calling "getproducts()" from your page ,so call to "products" is throwimg exception. Please try below below page code with same class. .
Let us know if it helps you.
try with below class code it will help . Let me know any issue .
Thanks
Manoj
I had tried with your code ,but I got the error message Error: Unknown property 'addcart.Displayproducts.name
I tried with you code, I got the error message Error: addcart Compile Error: Invalid constructor name: addcart at line 11 column 11
I tried with you code, I got the error message Error: addcart Compile Error: Invalid constructor name: addcart at line 11 column 11