You need to sign in to do that
Don't have an account?

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
Code that Errors Line 6
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; } } } }
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. PLease let us know if this will help you
Thanks
Amit Chaudhary
All Answers
Can you try setting Line 1 to:
Thanks.
Just put this before line no 6
It should work fine.
Thanks
Shashikant
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. PLease let us know if this will help you
Thanks
Amit Chaudhary