[SalesForce] Visualforce Error: “Return type of an Apex action method must be a PageReference. Found: core.apexpages.el.adapters.ApexMapELAdapter”

This is a follow-up question to: When attempting to use a Map I get "System.NullPointerException: Attempt to de-reference a null object"

I am now receiving "Return type of an Apex action method must be a PageReference. Found: core.apexpages.el.adapters.ApexMapELAdapter " when trying to return a Map value with a Integer and custom object to a Visualforce Page.

Visualforce Page:

<apex:page standardController="Opportunity_Package__c" extensions="MetadataPackageEditor" action="{!getPackageMap}">
  <apex:form >
      <apex:inputField value="{!Opportunity_Package__c.Opp_Package_Rel__c}" id="Opporunity"/>
      <apex:pageBlock title="Select Package(s)">
      <apex:pageMessages />
      <span>Name </span>
      <apex:inputField value="{!Opportunity_Package__c.Name}"/>

      <table style="width:100%;">
          <tbody>
              <tr>
                  <th>Select</th>
                  <th>Package</th>
                  <th>Products</th>
                  <th>PPU Discount</th>
                  <th>Expiration Date</th>
              </tr>

              <apex:repeat value="{!packageMap}" var="oppPackage">
              <tr>
                  <td></td>
                  <td><apex:inputField value="{!packageMap[oppPackage].Name}" /></td>
                  <td><apex:inputField value="{!packageMap[oppPackage].Products__c}" /></td>
                  <td><apex:inputField value="{!packageMap[oppPackage].PPU_Discount__c}" /></td>
                  <td></td>
              </tr>
              </apex:repeat>
          </tbody>
      </table>

      </apex:pageBlock>
  </apex:form>
</apex:page>

Code causing the issue:

Map<Integer, Package__c> packageMap { get; set; }


public Map<Integer, Package__c> getPackageMap() {

    Integer i = 0;

    packageMap = new Map<Integer, Package__c>();

    for (Package__c pack : [SELECT Name, Products__c, PPU_Discount__c FROM Package__c]) {
        packageMap.put(i, pack);
        i++;
        }

    return packageMap;

}

Opportunity Package is a junction record between Opportunity, and my custom object Package.
Package is pulled via lookup, and then queried for it's detailed information.

Best Answer

On line 1, you have an action set to {!getPackageMap}. An action function must return "void" or "null" (no value), or a valid PageReference. It cannot return any other data type, or you'll get an error message.

Since you're actually calling that function from apex:repeat, there's no reason why you need to call the action method. You should remove it from your markup on line 1.

<apex:page standardController="Opportunity_Package__c" extensions="MetadataPackageEditor" <!-- action="{!getPackageMap}" --> >
Related Topic