0

I've got quite a challenge while working with null values and a wrapper.

I query the database which sometimes might have null values, but when trying to create a new record (or actually assigning variables) I get exception. Code below.

  List<electricity_meter__c> MeterList = [SELECT Day_rate__c,Standing_Charge_yr__c 
                                          FROM ...]; 
  WrapperList = New List<WrapperClassEx>();
  for(electricity_meter__c met: MeterList){
      double currentStandingCharge = new double();
      if(met.get('Standing_Charge_yr__c')!=null){
          double currentStandingCharge = double.valueOf( met.get('Standing_Charge_yr__c') );
      } else {
          double currentStandingCharge = 0;
  }                    
  double currentDayRate = double.valueOf( met.get('Day_rate__c') );  
  WrapperList.add(New WrapperClassEx(currentDayRate,currentStandingCharge));

  for (WrapperClassEx wrap : WrapperList){
      lineItems.add(new Line_Items__c(                
          Current_Day_Rate__c=decimal.valueOf( wrap.currentDayRate ), 
          Current_Standing_Charge__c=decimal.valueOf(wrap.currentStanding)
      ));
  } 

If I leave code like this, I get Duplicate variable: currentStandingCharge (attempting to re-create the variable with type: double).

If I remove line double currentStandingCharge = new double();, I get Variable does not exist: currentStandingCharge.

If I remove the if() I get Argument 1 cannot be null Error is in expression '{!save}' in component <apex:commandButton> in page ... which by trial and error I found to be issue with adding fields to Line Items (last 4 lines in code excerpt).

I guess I am missing something really simple, just don't know what.

Any ideas?

  • Question: why do you bother using DOUBLEs? The data starts out as a DECIMAL, and you convert back to it anyways. It seems unecessary – Scott Pelak Apr 15 '14 at 15:09
  • This is funny one: Variable does not exist: decimal – dzh Apr 15 '14 at 15:22
4

Remove the word double You already declared these variables... so you don't want to redeclare them.

if(met.get('Standing_Charge_yr__c')!=null){
    double currentStandingCharge = double.valueOf( met.get('Standing_Charge_yr__c') );
} else {
    double currentStandingCharge = 0;
} 
|improve this answer|||||

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.