4

this one is a confidence killer... I must be doing something fundamentally wrong!

I have a list var in controller that I render to a pageblocktable. When I click 'update' custom button to save changes - my list var has been flushed back to null, and I get a 'null object' runtime error.

Any suggestions? Has me stumped.

VF:

<apex:pageblockbuttons> 
            <apex:commandButton value="Save" action="{!saveChanges}" immediate="false" rerender="msgs"/>
        </apex:pageblockbuttons>
            <apex:outputPanel id="theTable">
                <apex:pageblocktable value="{!templateList}" var="t">
                    <apex:column headervalue="Template Name" value="{!t.name}"/>
                    <apex:column headervalue="Template Description" value="{!t.description}"/>
                    <apex:column headervalue="Template Contents" ><apex:outputText value="{!t.htmlvalue}" escape="false"/></apex:column>
                    <apex:column headervalue="Available to Use" value="{!t.available}"/>
                    <apex:column headervalue="Dimension 1" ><apex:inputField value="{!t.et.pw_d1__c}"/></apex:column>
                    <apex:column headervalue="Dimension 2" ><apex:inputField value="{!t.et.pw_d2__c}"/></apex:column>
                    <apex:column headervalue="Dimension 3" ><apex:inputField value="{!t.et.pw_d3__c}"/></apex:column>
                </apex:pageblocktable>
            </apex:outputPanel>
    </apex:pageblock>

And the controller:

public with sharing class YourController {

    public static list<template> templateList {get; set;}

    public pageReference saveChanges(){
        // grab the records and upsert
        system.debug('templateList=>'+templateList); // this is proving templateList is null on arrival!
        List<pw_email_metadata__c> updList = new List<pw_email_metadata__c>();
        for(template t : templateList){
            updList.add(t.et);
        }
        if(!updList.isEmpty()) {
            try{
                upsert updList pw_templateId__c;
                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO,'Saved');
                ApexPages.addMessage(myMsg);
            } catch (exception e){
                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error on save:'+e);
                ApexPages.addMessage(myMsg);
            }
        }
        return null;
    }

    public class template {
        public pw_email_metadata__c et { get; set; }
    }
}
5
  • 1
    Can you share the rest of your page and controller? One thing I wonder, is how you have your templateList defined and exposed to the VF page? Also, you are rendering a msgs element, where is it on your page?
    – JimRae
    Oct 18, 2013 at 0:39
  • Whoops - the templatelist is declared as a public static ie. public static list<template> templateList {get; set;} template is a custom wrapper class. Oct 18, 2013 at 0:42
  • msgs is a <apex:pageMessages /> element - I tried to keep the bits that do work out of the question to try to limit noise. Oct 18, 2013 at 0:48
  • 1
    How is templateList initialised and assigned values? Oct 18, 2013 at 1:20
  • SaveChanges called from your Save button is trying to save a controller-instance (i.e. non-static) templateList. Basically, I'm not sure you can update a static templateList with your Save button, templateList should be non-static.
    – altius_rup
    Oct 27, 2013 at 6:54

2 Answers 2

2

Your templateList is declared as static, static variables are not included within the view state. This is covered in the documentation here.

Static methods, variables, or initialization code are associated with a class, and are only allowed in outer classes. When you declare a method or variable as static, it's initialized only once when a class is loaded. Static variables aren't transmitted as part of the view state for a Visualforce page.

1
  • Lol you stare at things long enough you don't see the obvious: thanks to all for the assistance! Apr 13, 2014 at 10:25
0

As @altius_rup mentioned in the comments, this should work fine if you remove the static keyword from the templateList member.

public list<template> templateList { get; set; }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .