Activity Forums Salesforce® Discussions Attempt to de-reference a null object apex

  • Attempt to de-reference a null object apex

    Posted by Pragya on July 22, 2020 at 4:17 pm

    Hi,
    I am getting attempt to de-reference a null object apex error while trying to allocate the record to a particular record type.
    Please help me out.
    Requirement:
    <div>As soon as the Return request record is created, automatically create "Voucher" record from the backend. For example - if Return request is approved for amount 5000, then there should be three Voucher records created automatically from backend (one record with record type 1000, two records with record type 2000)

    </div>
    public class returnRequestTriggerHandler
    {
    public void createVouchers(List<return_request__c> reqList)</return_request__c>
    {
    List<voucher__c> vList = new List<voucher__c>();</voucher__c></voucher__c>
    Decimal[] voucherNames=new Decimal[] {2000,1000,500,100,50,10,5,1};
    for(Return_Request__c r:reqList){
    Decimal price=r.Item_Original_Price__c;
    integer n=0;
    while(price>0)
    {
    Voucher__c newVoucher=new Voucher__c();
    if(price-voucherNames[n]>=0)
    {
    newVoucher.Name=String.valueOf(voucherNames[n].format())+' Voucher';
    newVoucher.Amount__c=voucherNames[n];
    newVoucher.Return_Request__c=r.Id;
    if(voucherNames[n]==2000){
    newVoucher.RecordTypeId= Schema.SObjectType.Voucher__c.getRecordTypeInfosByName()
    .get('X2000').getRecordTypeId();
    }
    else if(voucherNames[n]==1000){
    newVoucher.RecordTypeId= Schema.SObjectType.Voucher__c.getRecordTypeInfosByName()
    .get('X1000').getRecordTypeId();
    }
    else if(voucherNames[n]==500){
    newVoucher.RecordTypeId= Schema.SObjectType.Voucher__c.getRecordTypeInfosByName()
    .get('X500').getRecordTypeId();
    }
    else if(voucherNames[n]==100){
    newVoucher.RecordTypeId= Schema.SObjectType.Voucher__c.getRecordTypeInfosByName()
    .get('X100').getRecordTypeId();
    }
    else if(voucherNames[n]==50){
    newVoucher.RecordTypeId= Schema.SObjectType.Voucher__c.getRecordTypeInfosByName()
    .get('X50').getRecordTypeId();
    }
    else if(voucherNames[n]==10){
    newVoucher.RecordTypeId= Schema.SObjectType.Voucher__c.getRecordTypeInfosByName()
    .get('X10').getRecordTypeId();
    }
    else if(voucherNames[n]==5){
    newVoucher.RecordTypeId= Schema.SObjectType.Voucher__c.getRecordTypeInfosByName()
    .get('X5').getRecordTypeId();
    }
    else{
    newVoucher.RecordTypeId= Schema.SObjectType.Voucher__c.getRecordTypeInfosByName()
    .get('X1').getRecordTypeId();
    }
    vList.add(newVoucher);
    price=price-voucherNames[n];
    }
    else{
    n++;
    }
    }
    }
    insert vList;
    }
    }

    • This discussion was modified 4 years, 4 months ago by  Pragya.
    Pragya replied 4 years, 4 months ago 1 Member · 0 Replies
  • 0 Replies

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos

Popular Salesforce Infographics