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
rob_ocprob_ocp 

System.NullPointerException: Attempt to de-reference a null object

We are trying to do a trigger to calculate the amount in USD and store in a custom field in Opportunity. But I am facing the NullPointerException

Code:
trigger opptyAmountNusd on Opportunity (after update) {
//Calculates the USD equivalent and updates in another field

if (Trigger.isUpdate) {
    //for (Opportunity o_oppty : Trigger.old) {
   
    Opportunity[] opps = new List<Opportunity>();
        for (Opportunity n_oppty : Trigger.new) {
            Opportunity opp_tmp = n_oppty;
           
            if ((n_oppty.Amount != 0) ||(n_oppty.Amount != NULL) ) {
                    Decimal a = n_oppty.Amount;
                    String c = n_oppty.CurrencyIsoCode.substring(0,3);
                   
                    CurrencyType ct = [Select ConversionRate from CurrencyType where IsoCode=:c limit 1][0];
           
                    Decimal cr = ct.ConversionRate;
                   
                    opp_tmp.Amount_In_USD__c = cr*a;
                    opps.add(opp_tmp);
                   
                    //n_oppty.Amount_In_USD__c = cr*a;      
            }
        }
    //}
   
     for (Opportunity opp_tmp : opps) {
    
         update opp_tmp;
     }
}



}

Message Edited by rob_ocp on 07-10-2008 11:08 AM

Message Edited by rob_ocp on 07-10-2008 11:11 AM
Best Answer chosen by Admin (Salesforce Developers) 
canonwcanonw
This is only a hunch.  You need to confirm it on your part.

The culprit seems to be this line.

Code:
CurrencyType ct = [Select ConversionRate from CurrencyType where IsoCode=:c limit 1][0];

 
You may need to adjust it this way.

Code:
CurrencyType ct = null;
for( CurrencyType temp : [Select ConversionRate from CurrencyType where IsoCode=:c limit 1]) {
ct = temp;
}

....
// Do something with null ct.

 Let's know the result by posting the result.

All Answers

canonwcanonw
This is only a hunch.  You need to confirm it on your part.

The culprit seems to be this line.

Code:
CurrencyType ct = [Select ConversionRate from CurrencyType where IsoCode=:c limit 1][0];

 
You may need to adjust it this way.

Code:
CurrencyType ct = null;
for( CurrencyType temp : [Select ConversionRate from CurrencyType where IsoCode=:c limit 1]) {
ct = temp;
}

....
// Do something with null ct.

 Let's know the result by posting the result.

This was selected as the best answer
rob_ocprob_ocp
Status is same. I am still getting the same error message.
SuperfellSuperfell
give us a hint, what line does it say the error is on ?