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
Tyler HarrisTyler Harris 

Null Pointer Exception on Custom Class For Loop

I'm getting a null pointer exception on a for loop through list variable of a custom class. The loop was previously working  on an Sobject:

Class ProductsWrapper
global class ProductsWrapper {

    public Merchandise__c merchandise {get;set;}
    public Decimal count{get; set;}
    public Decimal tempCount{get;set;}
    
    public ProductsWrapper(Merchandise__c item){
        this.merchandise = item;
         
        
    }
    
    
    
    
}

Code that Errors Line 6
List<ProductsWrapper> products;
Map<Id, ProductsWrapper> cart;

public void handleTheBasket(){
      
        for(ProductsWrapper c : products){
            if(c.tempCount > 0){
            if(cart.containsKey(c.merchandise.Id)){        
                cart.get(c.merchandise.Id).count += c.tempCount;
                
            }
            else{
                cart.put(c.merchandise.Id, c);
                cart.get(c.merchandise.Id).count = c.tempCount;
                incart = true;
      
            } 
        
        }
        }
        
    }


 
Best Answer chosen by Tyler Harris
Amit Chaudhary 8Amit Chaudhary 8
Hi Tyler Harris,

Issue is coming because you are trying to access list of wrapper without creating object. It will better if you will add null check before using list

Try below code.
List<ProductsWrapper> products;
Map<Id, ProductsWrapper> cart;

public void handleTheBasket()
{
    if(products != null)
	{	
        for(ProductsWrapper c : products)
		{
            if(c.tempCount > 0)
			{
				if(cart.containsKey(c.merchandise.Id)){        
					cart.get(c.merchandise.Id).count += c.tempCount;
					
				}
				else{
					cart.put(c.merchandise.Id, c);
					cart.get(c.merchandise.Id).count = c.tempCount;
					incart = true;
		  
				} 
        
			}
        }
        
    }
}
PLease let us know if this will help you

Thanks
Amit Chaudhary
 

All Answers

Temoc MunozTemoc Munoz
How are products populated? From a Visual Force Page? Manually?

Can you try setting Line 1 to:
List<ProductsWrapper> products {get; set;}

Thanks.
Shashikant SharmaShashikant Sharma
Hi Tyler,

Just put this before line no 6
 
products = products != null : products ? new List<ProductsWrapper>();

It should work fine.

Thanks
Shashikant
Amit Chaudhary 8Amit Chaudhary 8
Hi Tyler Harris,

Issue is coming because you are trying to access list of wrapper without creating object. It will better if you will add null check before using list

Try below code.
List<ProductsWrapper> products;
Map<Id, ProductsWrapper> cart;

public void handleTheBasket()
{
    if(products != null)
	{	
        for(ProductsWrapper c : products)
		{
            if(c.tempCount > 0)
			{
				if(cart.containsKey(c.merchandise.Id)){        
					cart.get(c.merchandise.Id).count += c.tempCount;
					
				}
				else{
					cart.put(c.merchandise.Id, c);
					cart.get(c.merchandise.Id).count = c.tempCount;
					incart = true;
		  
				} 
        
			}
        }
        
    }
}
PLease let us know if this will help you

Thanks
Amit Chaudhary
 
This was selected as the best answer