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
bharath kuamarbharath kuamar 

I am getting the following test failure by running the test class

public with sharing class DemoRole 
{
    public List<OppData> list_opportunity {get; set;}
    public DemoRole()
    {
        list_opportunity = new List<OppData>();
        Set<Id> set_ownerId = new Set<Id>();
        List<OpportunityShare> oppList=[select Id,OpportunityId, Opportunity.Name, Opportunity.Owner.Name, UserOrGroupId from OpportunityShare Where Opportunity.Owner.UserRole.Name='UK Junior Representative'];
if(oppList!=null)
{
        for (OpportunityShare opp :oppList )
        {
               list_opportunity.add( getOppData(opp) );   
        }
}
getOppData(null);
        }
public static OppData getOppData(OpportunityShare opp)
{
            OppData oppData = new OppData();
    if(opp==null)
    System.debug('No record Found......');
            oppData.oppId = opp.OpportunityId;
            oppData.ownerName = opp.Opportunity.Owner.Name;
            oppData.managerName = opp.UserOrGroupId;
            oppData.oppName  = opp.Opportunity.Name;
    return oppData;
}
         
    
    public class OppData
    {
        public String oppId { get; set; }
        public String ownerName { get; set; }
        public String managerName { get; set; }
        public String oppName { get; set; }
    }
    
 // test class for the class demorole
 
 static testMethod void DemoRole ()
 {
DemoRole drule=new DemoRole();
OpportunityShare opp=new OpportunityShare();
opp.OpportunityId='00690000006yeky';
//opp.Opportunity.Owner.Name='sample owner';
opp.UserorGroupId='00690000006yeky';
opp.Opportunity.Name='test opportunity';
getOppData(opp);
 }
    
}

the test failure shows

Class.DemoRole.getOppData: line 23, column 1 Class.DemoRole.<init>: line 16, column 1 Class.DemoRole.DemoRole: line 43, column 1
please help me with this.........
Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

In this method:

 

public static OppData getOppData(OpportunityShare opp)
{
    OppData oppData = new OppData();
    if(opp==null)
    System.debug('No record Found......');
            oppData.oppId = opp.OpportunityId;
            oppData.ownerName = opp.Opportunity.Owner.Name;
            oppData.managerName = opp.UserOrGroupId;
            oppData.oppName  = opp.Opportunity.Name;
    return oppData;
}

 

You check if the opp passed in as a parameter is null (which it will be when called from the constructor as you explicitly pass in a null) and output some debug if it is.  Unfortunately you then start accessing fields on the null record.  If you are trying to null protect this method, you need to wrap the field access in an else condition, something like:

 

public static OppData getOppData(OpportunityShare opp)
{
            OppData oppData = new OppData();
    if(opp==null)
    {
            System.debug('No record Found......');
    }
    else
    {
            oppData.oppId = opp.OpportunityId;
            oppData.ownerName = opp.Opportunity.Owner.Name;
            oppData.managerName = opp.UserOrGroupId;
            oppData.oppName  = opp.Opportunity.Name;
    }
    return oppData;
}

 

 

 

 

All Answers

bob_buzzardbob_buzzard

In this method:

 

public static OppData getOppData(OpportunityShare opp)
{
    OppData oppData = new OppData();
    if(opp==null)
    System.debug('No record Found......');
            oppData.oppId = opp.OpportunityId;
            oppData.ownerName = opp.Opportunity.Owner.Name;
            oppData.managerName = opp.UserOrGroupId;
            oppData.oppName  = opp.Opportunity.Name;
    return oppData;
}

 

You check if the opp passed in as a parameter is null (which it will be when called from the constructor as you explicitly pass in a null) and output some debug if it is.  Unfortunately you then start accessing fields on the null record.  If you are trying to null protect this method, you need to wrap the field access in an else condition, something like:

 

public static OppData getOppData(OpportunityShare opp)
{
            OppData oppData = new OppData();
    if(opp==null)
    {
            System.debug('No record Found......');
    }
    else
    {
            oppData.oppId = opp.OpportunityId;
            oppData.ownerName = opp.Opportunity.Owner.Name;
            oppData.managerName = opp.UserOrGroupId;
            oppData.oppName  = opp.Opportunity.Name;
    }
    return oppData;
}

 

 

 

 

This was selected as the best answer
bharath kuamarbharath kuamar

thanks it worked