0

I have a requirement like need to display related opportunities for a specific lead with some criterias like sic,state,city.

1.sic starts with criteria like sic in lead is 123 i need to opportunities like 123,1234

2.Sic with state

3.Sic with state & city

4.State only

5.State & city

6.City

Because i need to cover all use cases.Unfortunately my code unable to handle 3, 5,6 conditions.

Please advise me how to fix.trimed version of my code.

public void search()
{
    set<String> cityset=new set<String>();
     codes = new Set<String>();
    Map<String,String> citymap=new Map<String,String>();
    Map<String,String> statemap=new Map<String,String>();
    Map<String,String> Siccodes=new Map<String,String>();
    newopplist=new List<Opportunity>();
    Secondopplist=new List<Opportunity>();
    MainSic=new List<Opportunity>();
    Mainlist=new List<Opportunity>();
    MainSic=null;
    Mainlist=null;
    Secondopplist = null;
    listopp=null;
    for(Lead ld:[Select Id,City,State,SIC_Code_1__c from Lead where Id=:CurrentId ])
    {
        // cityset.add(ld.City);
        if(ld.City <> null)
        {
            citymap.put(ld.City.toUpperCase(),ld.City);
        }
        if(ld.state <> null)
        {
            statemap.put(ld.State.toUpperCase(),ld.State);
        }
        if(ld.SIC_Code_1__c <> null)
        {
        Siccodes.put(ld.SIC_Code_1__c, ld.SIC_Code_1__c);
        }
    }
    newopplist=[Select Id,Name,Account.name,Number_of_Units__c,StageName,account.SIC_Code__c,Type,
                Billing_City__c,Billing_State_Province__c,account.SIC_Industry_Name__c,
                account.No_of_active_units__c from Opportunity where StageName='Closed Won' ];
    listopp=new List<Opportunity>();
    if(!Siccodes.isEmpty())
        { 
            for(String code:Siccodes.keySet())
            {
                codes.add(code + '%');
            }
        }
            for (Opportunity opp:[Select Id,Name,StageName,Account.name,Number_of_Units__c,account.SIC_Code__c,Type,
                     Billing_City__c,Billing_State_Province__c,account.SIC_Industry_Name__c,
                     account.No_of_active_units__c from opportunity where account.SIC_Code__c 
                     LIKE :codes ORDER BY account.No_of_active_units__c Desc NULLS LAST Limit 10])
            {
                if(opp.account.SIC_Code__c <> null)
                {
                    listopp.add(opp);
                }
            MainSic=listopp.clone();
            }  
    for(Opportunity opp: newopplist)
    {
         if(statemap.containskey(opp.Billing_State_Province__c) && listopp.size() > 0 && listopp!= null)
        {
            listopp.clear();
            for(Opportunity opps:[Select Id,Name,StageName,Account.name,Number_of_Units__c,account.SIC_Code__c,
                     Type,Billing_City__c,Billing_State_Province__c,account.SIC_Industry_Name__c,
                     account.No_of_active_units__c from Opportunity where account.SIC_Code__c 
                     LIKE :codes AND Billing_State_Province__c=:statemap.get(opp.Billing_State_Province__c) 
                     and StageName='Closed Won' ORDER BY account.No_of_active_units__c Desc NULLS LAST Limit 10])
            {
                System.debug('State Info with sic'+opps.Billing_State_Province__c);
                if(opps.Billing_State_Province__c <> null)
                {
                   listopp.add(opps); 
                }
                System.debug('Data in **** '+listopp);
                Mainlist=listopp.clone();
            }
        }  
        if(citymap.containsKey(opp.Billing_City__c) && Mainlist.size() > 0 && Mainlist!= null )
        {
            listopp.clear();
            for(Opportunity oppsd:[Select Id,Name,StageName,Account.name,Number_of_Units__c,account.SIC_Code__c,Type,
                     Billing_City__c,Billing_State_Province__c,account.SIC_Industry_Name__c,
                     account.No_of_active_units__c from Opportunity where account.SIC_Code__c 
                     LIKE :codes AND Billing_State_Province__c=:statemap.get(opp.Billing_State_Province__c) 
                     AND Billing_City__c =:citymap.get(opp.Billing_City__c) and 
                     StageName='Closed Won' ORDER BY account.No_of_active_units__c Desc NULLS LAST Limit 10])
            {
                if(oppsd.Billing_City__c <> null)
                {
                    listopp.add(oppsd);
                }
            }
        } 
        else if(statemap.containskey(opp.Billing_State_Province__c) && listopp.isEmpty() && listopp.size() < 1 )
        {
            listopp.clear();
            for(Opportunity opps:[Select Id,Name,StageName,Account.name,Number_of_Units__c,account.SIC_Code__c,
                             Type,Billing_City__c,Billing_State_Province__c,account.SIC_Industry_Name__c,
                             account.No_of_active_units__c from Opportunity where 
                             Billing_State_Province__c=:statemap.get(opp.Billing_State_Province__c) 
                             and StageName='Closed Won' ORDER BY account.No_of_active_units__c Desc NULLS LAST Limit 10])
            {
                if(opps.Billing_State_Province__c <> null) 
                {
                    listopp.add(opps); 
                }
            }
            Secondopplist=listopp.clone();
        }  
         if(citymap.containskey(opp.Billing_City__c) && Secondopplist.size() > 0 && Secondopplist != null)
        {
            listopp.clear();
            For(Opportunity opps:[Select Id,Name,StageName,Account.name,Number_of_Units__c,account.SIC_Code__c,
                     Type,Billing_City__c,Billing_State_Province__c,account.SIC_Industry_Name__c,
                     account.No_of_active_units__c from Opportunity where 
                     Billing_State_Province__c=:statemap.get(opp.Billing_State_Province__c) and 
                     Billing_City__c =:citymap.get(opp.Billing_City__c) and StageName='Closed Won' 
                     ORDER BY  account.No_of_active_units__c Desc NULLS LAST Limit 10])
            {
                if(opps.Billing_City__c <> null) 
                {
                    listopp.add(opps);
                }
            }
        } 
       else if(citymap.containskey(opp.Billing_City__c) && Secondopplist.size() < 1 && Secondopplist == null)
        {
            listopp.clear();
            for(Opportunity opps:[Select Id,Name,StageName,Account.name,Number_of_Units__c,account.SIC_Code__c,Type,
                     Billing_City__c,Billing_State_Province__c,account.SIC_Industry_Name__c,
                     account.No_of_active_units__c from Opportunity where 
                     Billing_City__c =:citymap.get(opp.Billing_City__c) and 
                     StageName='Closed Won' ORDER BY account.No_of_active_units__c Desc NULLS LAST Limit 10])
            {
                if(opps.Billing_City__c <> null)
                {
                    listopp.add(opps);
                }
            }
        } 
    }
}

My Stack Trace:

Time Started      6/22/2017 7:39 AM
Class             TestLeadController
Method Name       testwithSicandstatecity
Pass/Fail         Fail
Error Message   System.NullPointerException: Attempt to de-reference a null 
                 object
Stack Trace Class.LeadController.search: line 120, column 1
Class.TestLeadController.testwithSicandstatecity: line 102, column 1

And line 120 is

citymap.containskey(opp.Billing_City__c) && Secondopplist.size() > 0 && Secondopplist != null

1
2

There are three variables you de-reference in that line:

  • cityMap
  • opp
  • secondOppList

You try to do a null check on this third variable, but you have the order reversed. The other two variables do not appear likely to be null.

Protects From Null Pointer

if (secondOppList != null && !secondOppList.isEmpty())
{
    // do stuff
}

The above is essentially equivalent to:

if (secondOppList != null)
{
    // now you know the variable can be safely de-referenced
    // so the below line will never throw a NullPointerException
    if (!secondOppList.isEmpty())
    {
        // do stuff
    }
}

Adds No Protection

if (!secondOppList.isEmpty() && secondOppList != null)

The above is essentially equivalent to:

if (!secondOppList.isEmpty())
{
    // you've already de-referenced the variable
    // and hence hit your NullPointerException

    // the below check adds no value because it is too late
    if (secondOppList != null)
    {
        // do stuff
    }
}

You need to check null first, because short-circuit evaluation will prevent any de-reference attempt in that case.

0

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.