0

For some reason the variable does not get assigned.

What I am trying to do is to pass a set of ids to VF page from a list button.

Here's my VF code:

<apex:page StandardController="Electricity_Meter__c" extensions="findSupplier" recordSetVar="meters">
    <apex:pageblock >
      <apex:pageblockTable value="{!ApplicableMeterReturn}" var="Rec">
          <apex:column headerValue="Company Name" value="{!Rec.Supplier__r.name}"/>
      </apex:pageblockTable> 
    </apex:pageblock>  
</apex:page>

Here's the extension:

public class findSupplier {
      Public      List<sobject>           metersToQuote      {get;set;}  
    public findSupplier(ApexPages.StandardSetController controller) {
      mycon = controller;
      metersToQuote = mycon.getSelected();
    }
    ApexPages.StandardSetController mycon;
    List<electricity_meter__c> MeterList = [Select id,name,AQ__c,meter_details__c,Profile_Class__c 
                                            from electricity_meter__c where (id IN: metersToQuote)]; 


    public list<applicable_meter__c> getApplicableMeterReturn(){    
          list<Quote__c> oldQuotes = new list<Quote__c>();
          list<applicable_meter__c> applicableMeters = new list<applicable_meter__c>();

          string supplierQueryString = 'select Supplier__r.name from Applicable_Meter__c ';
          string supplierGroupModifier = ' group by Supplier__r.name';
          string supplierWhereCondition;

          for (sobject meters: MeterList){
                if (supplierWhereCondition == null) {
                    supplierWhereCondition = 'WHERE ';
                } else {
                    supplierWhereCondition += ' OR ';
                }
              string meterAQ = string.valueOf(meters.get('AQ_C'));
              supplierWhereCondition += '(min_aq__c => \'' + meterAQ + '\'      )           ';
          }
    applicableMeters = Database.query(supplierQueryString + supplierWhereCondition + supplierGroupModifier);   
    return applicableMeters;
    } 
}

EDIT:

I've pulled the error from the debug log:

15:44:08.088 (88086257)|SOQL_EXECUTE_BEGIN|[8]|Aggregations:0|select id, name, AQ__c, meter_details__c, Profile_Class__c from electricity_meter__c where id IN :tmpVar1
15:44:08.090 (90911470)|EXCEPTION_THROWN|[8]|System.NullPointerException: Attempt to de-reference a null object
  • What line is the error happening? What debugging have you done? Your title mentions tmpVar1 but there is no tmpVar1 variable in your code. – BarCotter Jul 22 '14 at 14:52
  • Good point. I've pulled this from the debug log. – dzh Jul 22 '14 at 14:53
  • salesforce.stackexchange.com/questions/36582/… would be a good place to start – BarCotter Jul 22 '14 at 14:54
4

You are performing the query that references metersToQuote outside of any method, so I presume what is happening is that that code is being executed before the body of the constructor that initialises metersToQuote.

Test case that illustrates that the constructor runs later than the field initialisation:

@isTest
private class MyTest {
    private class MyClass {
        Integer i = 1;
        MyClass() {
            i = 2;
            j = 4;
        }
        Integer j = 3;
    }
    @isTest
    static void test() {
        MyClass c = new MyClass();
        System.assertEquals(2, c.i);
        System.assertEquals(4, c.j);
    }
}
| improve this answer | |
  • You are right. I've moved the query into getApplicableMeterReturn method and it solved the problem. – dzh Jul 22 '14 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.