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
Arun Yadav 13Arun Yadav 13 

Error : attempt to de-reference null object

Hi,

I'm getting null point exception error : attempt to de-reference object which is null at below line.

Utils.getQuoteIdsForSupportItems(newLst,oldLst);

/*test class
  QuoteLineItem qli1 = new QuoteLineItem();
  qli1.QuoteId = q.Id;
  qli1.PricebookEntryId =Pbentry.Id;
  qli1.Discount=91;         
  qli1.Quantity=8;
  qli1.Product2Id = p.Id;
  qli1.UnitPrice=98;
  qli1.Description =p.Description;  
//  insert qli1;

  QuoteLineItem qli2 = new QuoteLineItem();
  qli2.QuoteId = q.Id;
  qli2.PricebookEntryId =Pbentry.Id;
  qli2.Discount=92;         
  qli2.Quantity=1;
  qli2.Product2Id = p.Id;
  qli2.UnitPrice=94;
  qli2.Description =p.Description;  
//  insert qli2;
 
  QuoteLineItem qli3 = new QuoteLineItem();
  qli3.QuoteId = q.Id;
  qli3.PricebookEntryId =Pbentry.Id;
  qli3.Discount=92;         
  qli3.Quantity=1;
  qli3.Product2Id = p.Id;
  qli3.UnitPrice=94;
  qli3.Description =p.Description;  
//  insert qli3;

  List<QuoteLineItem> oldLst = new List<QuoteLineItem>();
  oldLst.add(qli1);
  oldLst.add(qli2);
  oldLst.add(qli3);
  insert oldLst;
 
  QuoteLineItem qli4 = new QuoteLineItem();
  qli4.QuoteId = q.Id;
  qli4.PricebookEntryId =Pbentry.Id;
  qli4.Discount=41;         
  qli4.Quantity=7;
  qli4.Product2Id = p.Id;
  qli4.UnitPrice=99;
  qli4.Description =p.Description;  
//  insert qli4;

  QuoteLineItem qli5 = new QuoteLineItem();
  qli5.QuoteId = q.Id;
  qli5.PricebookEntryId =Pbentry.Id;
  qli5.Discount=23;         
  qli5.Quantity=10;
  qli5.Product2Id = p.Id;
  qli5.UnitPrice=81;
  qli5.Description =p.Description;  
//  insert qli5;

  QuoteLineItem qli6 = new QuoteLineItem();
  qli6.QuoteId = q.Id;
  qli6.PricebookEntryId =Pbentry.Id;
  qli6.Discount=24;
  qli6.Quantity=11;
  qli6.Product2Id = p.Id;
  qli6.UnitPrice=82;
  qli6.Description =p.Description;  
//  insert qli6;

  List<QuoteLineItem> newLst = new List<QuoteLineItem>();
  newLst.add(qli4);
  newLst.add(qli5);
  newLst.add(qli6);
  insert newLst;

Utils.getQuoteIdsForSupportItems(newLst,oldLst);

/*method to test..
  public static Set<Id> getQuoteIdsForSupportItems(List<QuoteLineItem> newList, List<QuoteLineItem> oldList) {
    Set<Id> prodIds = new Set<Id>();
    if(trigger.isInsert || trigger.isUpdate) {
      for(QuoteLineItem qli : newList) {
        prodIds.add(qli.Product2Id);
      }
    } else {
      for(QuoteLineItem qli : oldList) {
        prodIds.add(qli.Product2Id);
      }
    }
    
    Map<Id, Product2> prodMap = new Map<Id, Product2>([select Requires_Support_Fees__c
                        from Product2
                        where id in:prodIds
                        and Requires_Support_Fees__c = true]);
    Set<Id> quoteIds = new Set<Id>();
    if(trigger.isInsert || trigger.isUpdate) {
      for(QuoteLineItem qli : newList) {
        if(prodMap.containsKey(qli.Product2Id)) {
          quoteIds.add(qli.QuoteId);
        }
      }
    } else {
      for(QuoteLineItem qli : oldList) {
        if(prodMap.containsKey(qli.Product2Id)) {
          quoteIds.add(qli.QuoteId);
        }
      }
    }
    system.debug('quoteIds:'+quoteIds);
    return quoteIds;
  }


Please advice..

Arun
Medhya MahajanMedhya Mahajan
Hi ,

Change you method to test as below:
 
public static Set<Id> getQuoteIdsForSupportItems(List<QuoteLineItem> newList, List<QuoteLineItem> oldList) {
    Set<Id> prodIds = new Set<Id>();
    if(trigger.isInsert || trigger.isUpdate) {
			for(QuoteLineItem qli : newList) {
			if(qli.Product2Id !=null)
			prodIds.add(qli.Product2Id);
		  }
    }else {
		  for(QuoteLineItem qli : oldList) {
			if(qli.Product2Id !=null)
			prodIds.add(qli.Product2Id);
      }
    }
    
    Map<Id, Product2> prodMap = new Map<Id, Product2>([select Requires_Support_Fees__c
                        from Product2
                        where id in: prodIds
                        and Requires_Support_Fees__c = true]);
    Set<Id> quoteIds = new Set<Id>();
    if(trigger.isInsert || trigger.isUpdate) {
      
	  for(QuoteLineItem qli : newList) {
	  
        if(Product2Id != null && QuoteId != null && prodMap.containsKey(qli.Product2Id) ) {
          quoteIds.add(qli.QuoteId);
        }
      }
    } else {
      for(QuoteLineItem qli : oldList) {
        if(Product2Id != null && QuoteId != null && prodMap.containsKey(qli.Product2Id)) {
          quoteIds.add(qli.QuoteId);
        }
      }
    }
    system.debug('quoteIds:'+quoteIds);
    return quoteIds;
  }

Kindly ignore syntax errors if any. 

Please mark this a solved if this helps.

Regards
Medhya Mahajan
Arun Yadav 13Arun Yadav 13
Hi Medhya,

Thanks for the answers.
However I can't change the class code, I have to write test class. Can you please make changes in test class to solve if possible.

Arun.
Arun Yadav 13Arun Yadav 13
Hi Medhya

Can you please change the code of test class instead please.

Thanks
Arun
Medhya MahajanMedhya Mahajan
Arun,

The flaw is in the main class itself. There is probably no product being created beacuse of which the Product2Id seems to be null. You have not included the complete code for your test class where you are creating the product. 

Please ensure that the product (p) in line qli6.Product2Id = p.Id; is actually getting inserted.

Also, it is advisable to make changes in the main class because the scenario can come up whenever there is no product (not just your test class) and this will result in your functionality to fail.

Regards
Medhya Mahajan
 
Arun Yadav 13Arun Yadav 13
Hi Medhya,

Thanks for your help. Here is the code for product in test class.
Please let me know where should I modify test class, I don't have permission to edit main class hence I don't want to modify it.

  Product2 p = new Product2();
  p.Name='Support Fees';
  p.IsActive =true;
  p.Product_Family__c = pf.Id;
  p.Description='hello';
  p.Requires_Support_Fees__c=True;
  insert p;

Arun
Shivani BankarShivani Bankar
hello,
I'm having same error with following code

public class StringChecking{
    
    public String name1{get; set;}
    public List<contact> list2;
    Contact contact1;
    public Contact getContact(){
        if(contact1!=null){
        contact1=[select id,Name,Country__c from Contact where Name=:name1];
    }
        return contact1;
    }
    
    public pagereference ContactList(){
       return null;
    }
    public List<Contact> getList(){
        try{
            if(contact1!=null && contact1.Country__c=='India'){
                list2.add(contact1);                
            }
            
            else if(contact1!=null && contact1.Country__c=='US'){
                 list2.add(contact1);
            }
            
            else{
                 list2.add(contact1);
            }
        }
        catch(DmlException e)
        {
            System.debug('An unexpected error has occurred: ' + e.getMessage());
        }
        return list2;
    }
    
    public pagereference edit(){
        pagereference pg=new Pagereference('https://ap5.salesforce.com/'+contact1.Id);
        return pg;
    }
    
}


please help me to solve this