function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Harry MoeungHarry Moeung 

Attempt to de-reference a null object Error

Hello Devs,

I am getting a
'System.NullPointerException: Attempt to de-reference a null object' error when attempting to add values to a list of an object. I am not quite sure what is causing this error but I don't believe I'm passing a null value into the list. I need to add items to the oandaExchangeRateList invocable variable so I can pass that into an Apex Flow.


//This is the wrapper class. Rmoved a lot of code for readability.
public with sharing class OandaExchangeRatesCalloutsWrapperV1 {

public class methodOutputs{
        @InvocableVariable
        public List<OandaExchangeRate__c> oandaExchangeRateList;
    }
}


//This is the debug code that gives me the Null Pointer Exception Error
OandaExchangeRate__c oandaExchangeRate = new OandaExchangeRate__c();

oandaExchangeRate.Base_Currency__c = 'USD';
oandaExchangeRate.Quote_Currency__c = 'USD';
oandaExchangeRate.Average_Exchange_Rate__c = 1.0;

OandaExchangeRatesCalloutsWrapperV1.methodOutputs wrapper = new OandaExchangeRatesCalloutsWrapperV1.methodOutputs();
wrapper.oandaExchangeRateList.add(oandaExchangeRate);


 
Abdul KhatriAbdul Khatri
Hi Harry,

Please change here
public class methodOutputs{
        @InvocableVariable
        public List<OandaExchangeRate__c> oandaExchangeRateList = new List<OandaExchangeRate__c>();
    }
}
I hope this help
 
Maharajan CMaharajan C
Hi Hary,

You have to initiate the oandaExchangeRateList like below:
 
OandaExchangeRate__c oandaExchangeRate = new OandaExchangeRate__c();

oandaExchangeRate.Base_Currency__c = 'USD';
oandaExchangeRate.Quote_Currency__c = 'USD';
oandaExchangeRate.Average_Exchange_Rate__c = 1.0;

OandaExchangeRatesCalloutsWrapperV1.methodOutputs wrapper = new OandaExchangeRatesCalloutsWrapperV1.methodOutputs();
wrapper.oandaExchangeRateList = new List<OandaExchangeRate__c>{oandaExchangeRate};

Thanks,
Maharajan.C
Harry MoeungHarry Moeung
Hi Abdul and Maharajan,

Thanks for the help. Both solutions worked!