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?
Attribution to: dzh
Possible Suggestion/Solution #1
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;
}
Attribution to: dphil
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/32771