+ Start a Discussion
Tyler HarrisTyler Harris 

Create Counter Variable That Increments On Visualforce Page

Hello,

I want to increment a Counter Variable that is incremented when products are added to a list. I've been failing at doing this. Either it throws a Null Pointer exception on the Visualforce page, or it doesn't show any incrementing in the OutPutPanel. Any assistance would be helpful.

Thanks.

Apex
public class StoreFront {

    public String message { get; set; }
    public Integer shopCart{get;set;}
    
   

    
    public PageReference shop(){
        shopCart = 0;
    message = 'You bought: ';
        for (DisplayMerchandise p:products){
            if(p.count > 0){
               message += p.merchandise.name + ' (' + p.count + ') ' ;
                shopCart++;
            }
        }
        return null;
    }
    
    DisplayMerchandise[] products;
  
        
   
    

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

}
VF
<apex:page standardStylesheets="false" showHeader="false" sidebar="false" controller="StoreFront">
  <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="buy" reRender="msg"/>
  </apex:form>
  
  <apex:outputPanel id="msg">{!message}</apex:outputPanel>    
 
    <apex:outputPanel id="cart">{!shopCart}</apex:outputPanel>
  
</apex:page>




 
Best Answer chosen by Tyler Harris
Big EarsBig Ears
Tyler,

This might not be the answer you're looking for, but I've replicated your page and I'm getting it to refresh. Does your version of the page have more mark-up than this? Could there be page or record validations that are being triggered by the refresh? Try adding immediate="true" to your commandbutton and seeing if that produces a different behaviour?

Andy

All Answers

Big EarsBig Ears
Tyler,

This might not be the answer you're looking for, but I've replicated your page and I'm getting it to refresh. Does your version of the page have more mark-up than this? Could there be page or record validations that are being triggered by the refresh? Try adding immediate="true" to your commandbutton and seeing if that produces a different behaviour?

Andy
This was selected as the best answer
Tyler HarrisTyler Harris
That did it! Why is the immediate="true" attribute needed? It should just work. Very strange.
Big EarsBig Ears
Hey Tyler,

'Immediate=true' bypasses page validations, but it also prevents anything being committed to the server. As a result, it's only useful in certain situations. For example, you might use it on a "cancel" button on a page with a lot of required fields. You don't want your user to have to fill in all of the required fields before cancelling, so use it there.

However, in your case, I wonder if you've got additional markup with required fields, etc. In those cases, I'll bet those requirements are preventing your page refresh. You'll have to find a way of working around those fields that makes sense in your context.

Andy