You need to sign in to do that
Don't have an account?
Tyler Harris
Add List Item to another List (Display in Visualforce)
I'm trying to add my products from my DisplayMerchandise[] List to a shopping cart called "cart". How can I accomplish this? Every time I hit "Add to Cart" it throws a null pointer exception.
Apex
Visualforce
Apex
public class StoreFront2 { public String message { get; set; } DisplayMerchandise[] products; DisplayMerchandise[] cart; public PageReference shop(){ message = 'You bought: '; for (DisplayMerchandise p:products){ if(p.count > 0){ message += p.merchandise.name + ' (' + p.count + ') ' ; Decimal counts = p.count; } cart.addAll(p); } return null; } public DisplayMerchandise[] getcart(){ if(cart != null){ cart = new DisplayMerchandise[]{}; } return cart; } public class DisplayMerchandise { public Merchandise__c merchandise{get; set;} public Decimal count{get; set;} public DisplayMerchandise(Merchandise__c item){ this.merchandise = item; } } public DisplayMerchandise[] getProducts() { if (products == null){ products = new DisplayMerchandise[]{}; for (Merchandise__c item : [SELECT id, name, description__c, price__c FROM Merchandise__c WHERE Total_Inventory__c > 0]) { products.add(new DisplayMerchandise(item)); } } return products; } }
Visualforce
<apex:page standardStylesheets="false" showHeader="false" sidebar="false" controller="StoreFront2"> <apex:stylesheet value="{!URLFOR($Resource.styles)}"/> <h1>Store Front</h1> <apex:form > <apex:dataTable value="{!products}" var="pitem" rowClasses="odd,even"> <apex:column headerValue="Product"> <apex:outputText value="{!pitem.merchandise.name}" /> </apex:column> <apex:column headervalue="Price"> <apex:outputText value="{!pitem.merchandise.Price__c}" /> </apex:column> <apex:column headerValue="#Items"> <apex:inputText value="{!pitem.count}"/> </apex:column> </apex:dataTable> <br/> <apex:commandButton action="{!shop}" value="Add to Cart" reRender="msg,cart" /> </apex:form> <apex:outputPanel id="msg">{!message}</apex:outputPanel><br/> <h1>Your Basket</h1> <form> <apex:dataTable id='cart' value="{!cart}" var="carti" rowClasses="odd,even"> <apex:column headerValue="Product"> <apex:outputText value="{!carti.merchandise.name}" /> </apex:column> <apex:column headervalue="Price"> <apex:outputText value="{!carti.merchandise.Price__c}" /> </apex:column> <apex:column headerValue="#Items"> <apex:outputText value="{!carti.count}"/> </apex:column> </apex:dataTable> </form> </apex:page>
Modify this -
public DisplayMerchandise[] getCart() {
if(cart != null){
cart = new DisplayMerchandise[]{};
}
return cart;
}
to -
public DisplayMerchandise[] getCart() {
if(cart == null){
cart = new DisplayMerchandise[]{};
}
return cart;
}
All Answers
Modify this -
public DisplayMerchandise[] getCart() {
if(cart != null){
cart = new DisplayMerchandise[]{};
}
return cart;
}
to -
public DisplayMerchandise[] getCart() {
if(cart == null){
cart = new DisplayMerchandise[]{};
}
return cart;
}
Add a constructor just under the cart variable...