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
Tyler HarrisTyler 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
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>

 
Best Answer chosen by Tyler Harris
JayantJayant
You have created a new reference to cart when its not null. Not sure whether that is required or not but you should definitely do it when its null.

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

JayantJayant
You have created a new reference to cart when its not null. Not sure whether that is required or not but you should definitely do it when its null.

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;
}

This was selected as the best answer
Tyler HarrisTyler Harris
Thank you Jayant! 
Leslie  KismartoniLeslie Kismartoni
The trouble is that cart isn't initialized... You should have a constructor that initializes the cart.

Add a constructor just under the cart variable... 
 
DisplayMerchandise[] cart;

 
    public StoreFront2() {
        cart = new List<DisplayMerchandise>();
    }


 
Leslie  KismartoniLeslie Kismartoni
Jayant beat me to the punch...