Not able to get CW list value in pageref method.

    public  wrapperClassController1() { 
    **List<ProductWrapper1> cw;**
    searchResultsWithWrapper = new List<ProductWrapper>();
    ProductsList = [Select Id, Name, ProductCode, Description from Product2 where isActive=true limit 1];
    theOpp = [select Id, Pricebook2Id, PriceBook2.Name from Opportunity where Id = :ApexPages.currentPage().getParameters().get('id') limit 1];
    p=[SELECT Description,Family,Id,Name,Parent_Lookup__c,ProductCode,(SELECT Description,Family,Id,Name,Parent_Lookup__c,ProductCode FROM Products__r where Parent_Lookup__c!=null  ) FROM Product2 where Parent_Lookup__c=null  ];
    if(ProductsList  != null){     
    for(Product2 product:p){
    cw=new list<productwrapper1>();
    for(Product2 product1:product.Products__r){
    cw.add(new ProductWrapper1(product1));
    searchResultsWithWrapper.add(new ProductWrapper(product,false,cw));

    public PageReference proceedToConfigure() 

//not able to enter loop as cw is null.
    for(ProductWrapper1 productWrapper12 :cw){
     productIdMap1.put(productWrapper12.product.Id, productWrapper12.product.Id);
    List<PriceBookEntry> priceBookEntries = [Select Id, Product2Id,Product2.Name,priceBookEntry.Name,UnitPrice from PriceBookEntry where Pricebook2.IsStandard = true 
                    and Product2.isActive = true and Product2Id in :productIdMap1.keySet()];
    if(priceBookEntries != null){
    for(PriceBookEntry priceBookEntry : priceBookEntries ){
    productIdPriceMap1.put(priceBookEntry.Product2Id , priceBookEntry.UnitPrice);
    productIdPriceBookEntryIdMap1.put(priceBookEntry.Product2Id ,priceBookEntry.Id);
    productIdPriceName1.put(priceBookEntry.Product2.Name , priceBookEntry.Name );


And this my wrapper:

public class ProductWrapper1{
        public Product2 product{get;set;}
        public boolean selected{get;set;}

        public ProductWrapper1(Product2 a){


public class ProductWrapper{
public Product2 product{get;set;}
public boolean selected{get;set;}   
public List<ProductWrapper1> prodset{get;set;}      
public ProductWrapper(Product2 p,boolean b,List<ProductWrapper1> c){
  • 1
    Your copy-pasted code is kind of garbled. Perhaps you could try it again? – sfdcfox Sep 25 '15 at 17:15
  • It is not copy pasted .Neither it is garbled . – Learner Sep 25 '15 at 17:22
  • 2
    Your whitespace is all over the place, lines are merged together, and the entire thing is markdown quoted. I can barely figure out the difference between a class, method, variable, etc. I'm not in the habit of re-writing an entire post just so I can try to figure out what's going on. – sfdcfox Sep 25 '15 at 17:25
  • Oops I have made changes – Learner Sep 25 '15 at 17:40

Your initialization is within the for-each loop that loops over the query result. Assuming your query returns zero rows, this will mean that cw remains uninitialized. This suggests that you have at least two problems: (1) the cw = new list<productwrapper1>(); code needs to be moved up a line (outside the loop), and (2) your query is returning no results, so you'll want to check your filters/data.

Other general notes:

  • Query results are never null.
  • Don't use try-catch blocks indiscriminately.
  • Don't litter your code with debug statements.
  • If you must use try-catch blocks, try to be more descriptive than 'Oops!'. Consider using ApexPages.addMessages(e); to report the errors directly.
  • Taking your code, and comments, at face value, consider cleaning up your curly braces, commas, parenthesis, and so on. This code is not easy to read, which makes it harder to maintain.
  • Thanks for reply I understand there are lot of bugs in my code. I cannot initialize this cw = new list<productwrapper1>(); out of for .Becoz m fetching parent and child info on my page. – Learner Sep 25 '15 at 17:50
  • @Learner Irrelevant. You should initialize your list outside of the for loop-- if you fail to, it will be wiped out when there's more than one parent. Just try it and see. – sfdcfox Sep 25 '15 at 18:41
  • Thanks for reply .If i use it outside loop , child items are shown in every parent item.And if i use inside for loop it is working fine. – Learner Sep 26 '15 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.