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
KNKKNK 

Attempt to derefrence a null object...

Hi Team,

 

Could you please any one help me with below code.

 

I am getting attempt derernce a null object  at this part

 

public PageReference addProducts() {
        List<Case_Product__c> cpsToBeInserted = new List<Case_Product__c>();
        
        for (LabelWrapper clw : labels) {
            if (clw.isSelected) {
                system.debug('Naresh');
                cpsToBeInserted.add(new Case_Product__c(Case__c = caseId, Product_Shipper__c = clw.cl.Id));
            }
        }
   

 

------------------------------------------------------------------------------------------

 


public with sharing class AddCaseProducts {

    public List<LabelWrapper> labels{get; set;}
    public List<SelectOption> productFamilies{get; set;}
    public String productFamily{get; set;}
    Id caseId;
    
    public AddCaseProducts(ApexPages.StandardController controller) {
        caseId = ApexPages.currentPage().getParameters().get('caseId');
        productFamilies= new List<SelectOption>();
        productFamily= '';
        
        for (PicklistEntry pe : Schema.SObjectType.Product_Shipper__c.Fields.Family__c.getpicklistValues()) {
            productFamilies.add(new SelectOption(pe.getValue(), pe.getLabel()));
        }
    }
    
    public void initialize() {
        if (caseId != null) {
            labels = new List<LabelWrapper>();
            //String campaignType = [select RecordType.Name from Campaign where Id=:campaignId].RecordType.Name;
            List<Id> existingCPs = new List<Id>();
            
            for (Case_Product__c cp : [SELECT Product_Shipper__c FROM Case_Product__c WHERE Case__c = :caseId])
                existingCPs.add(cp.Product_Shipper__c);
            String query = 'SELECT Id, Name, Family__c,AGI_Code__c,Pack_Size__c,Price__c FROM Product_Shipper__c WHERE Id NOT IN :existingCPs';
            if (productFamily!= null && productFamily!= '') {
                query += ' AND Family__c INCLUDES (:productFamily)';
                
                system.debug('Naresh');
            }
            List<Product_Shipper__c> prodList = Database.query(query);
            for (Product_Shipper__c l : prodList)
                labels.add(new LabelWrapper(l));
            if (labels.isEmpty()) {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.INFO, 'There are no products to display'));
            }
        }
        else {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.FATAL, 'Please open this page from Case detail page'));
        }
    }
    
    public PageReference addProducts() {
        List<Case_Product__c> cpsToBeInserted = new List<Case_Product__c>();
        
        for (LabelWrapper clw : labels) {
            if (clw.isSelected) {
                system.debug('Naresh');
                cpsToBeInserted.add(new Case_Product__c(Case__c = caseId, Product_Shipper__c = clw.cl.Id));
            }
        }
        
        if (!cpsToBeInserted.isEmpty()) {
            try {
                insert cpsToBeInserted;
                
                return new ApexPages.StandardController(new Case(Id = caseId)).view();
            }
            catch(System.DMLException e) {
                ApexPages.addMessages(e);
            }
        }
        else {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.FATAL, 'Please select atleast a product to insert or click cancel to go back'));
        }
        return null;
    }
    
    public PageReference cancel() {
        if (caseId != null) {
            return new ApexPages.StandardController(new Case(Id = caseId)).view();
        }
        return new PageReference('/home/home.jsp');
    }
    
    class LabelWrapper {
        public boolean isSelected{get; set;}
        public Product_Shipper__c cl {get; set;}
        
        public LabelWrapper(Product_Shipper__c cl) {
            this.cl = cl;
        }
    }

}

ibtesamibtesam

public PageReference addProducts() {
        List<Case_Product__c> cpsToBeInserted = new List<Case_Product__c>();
        
        for (LabelWrapper clw : labels) {
            if (clw.isSelected) {
                system.debug('Naresh');
                cpsToBeInserted.add(new Case_Product__c(Case__c = caseId, Product_Shipper__c = clw.cl.Id));
            }
        }
  

probably that clw.cl dont have id? i mean that cl value is null for that clw?