function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
manjunath vivekmanjunath 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.
<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
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi ManjuNath,

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. .
<apex:page standardController="product2" showheader="false" sidebar="false" recordSetVar="Product2" extensions="addcart">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!products}" 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>

Let us know if it helps you.
ManojjenaManojjena
Hi Manjunath ,

try with below class code it will help .
Public class addcart {
	Displayproducts[] products;
	public class Displayproducts {
	public product2 newproduct { get; set; }
	public Decimal count{ get; set; }
	public String message { get; set; }
   public addcart(ApexPages.StandardSetController controller) {
      message='';
	  Products=new Displayproducts[]{};
    }
public PageReference additems() {
	message = 'You bought: ';
	for (Displayproducts p: products) {
		if (p.count> 0) {
			message += p.newproduct.name ; 
			System.debug('test'+message);
		}
	}
	return null;
}

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;
   }
}
Let me know any issue .
Thanks
Manoj
 
manjunath vivekmanjunath vivek
Hi Ashish,

I had tried with your code ,but I got the error message Error: Unknown property 'addcart.Displayproducts.name
manjunath vivekmanjunath vivek
Hi Manoj,

I tried with you code, I got the error message Error: addcart Compile Error: Invalid constructor name: addcart at line 11 column 11
manjunath vivekmanjunath vivek
Hi Manoj,

I tried with you code, I got the error message Error: addcart Compile Error: Invalid constructor name: addcart at line 11 column 11