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
MrHammyMrHammy 

A little help with a test class for a visualforce page

Working on a test class and am missing something basic, error is System.NullPointerException: Attempt to de-reference a null object  and the stack trace is Class.programclone.<init>: line 39, column 1
Class.testclonedppage.test_method_one: line 52, column 1

VF page 
 
<apex:page Controller="programclone" >
 


     <apex:form id="frm1" >
     

     
        <apex:pageblock >
        
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!Save}" />
                <apex:commandButton value="Cancel" action="{!cancel}" />
            </apex:pageBlockButtons>
            
            
                    <apex:pageBlockTable value="{!Records}" var="Record"> 
            <apex:column > 
                <apex:facet name="header">Account Name</apex:facet> 
                <apex:outputText value="{!Record.Account_Name__r.name}"/> 
            </apex:column> 
            
                        <apex:column > 
                <apex:facet name="header">EWS Territory</apex:facet> 
                <apex:outputText value="{!Record.Account_Name__r.Territory_EWS__c}"/> 
            </apex:column> 
            
            <apex:column > 
                <apex:facet name="header">City</apex:facet> 
                <apex:outputText value="{!Record.Account_Name__r.billingcity}"/> 
            </apex:column> 
            
            <apex:column > 
                <apex:facet name="header">State</apex:facet> 
                <apex:outputText value="{!Record.Account_Name__r.billingState}"/> 
            </apex:column> 
            
            <apex:column > 
                <apex:facet name="header">Key Contact</apex:facet> 
                <apex:outputText value="{!Record.DIST_EXEC_SPONSOR__r.firstname}"/> 
            </apex:column> 

            <apex:column > 
                <apex:facet name="header">Email</apex:facet> 
                <apex:outputText value="{!Record.DIST_E_S_Email__c}"/> 
            </apex:column>             
            <apex:column > 
                <apex:facet name="header">Program Type </apex:facet> 
                <apex:outputText value="{!Record.RecordType.name }"/> 
            </apex:column> 
            
            <apex:column > 
                <apex:facet name="header">Status</apex:facet> 
                <apex:outputText value="{!Record.Program_Status__c }"/> 
            </apex:column> 
            
            <apex:column > 
                <apex:facet name="header">100%</apex:facet> 
                <apex:Outputtext value="{!Record.CS_100PercentCommitted__c }"/> 
            </apex:column> 
           
           
           
             <apex:column > 
                <apex:facet name="header">YTD Sales</apex:facet> 
              <apex:Outputtext value="{!Record.CS_YTD_Sales__c}"/> 
            </apex:column>  
            
            <apex:column > 
                <apex:facet name="header">Run Rate</apex:facet> 
               <apex:Outputtext value="{!Record.CS_Run_Rate__c}"/> 
            </apex:column>  
                         
            <apex:column > 
                <apex:facet name="header">Projected Payout</apex:facet> 
               <apex:Outputtext value="{!Record.CS_Projected_Payout__c}"/> 
            </apex:column>  
            
            
            <apex:column > 
                <apex:facet name="header">Clone</apex:facet> 
                <apex:inputField value="{!Record.Clone_no_send__c}"/> 
            </apex:column> 
            
            
                        <apex:column > 
                <apex:facet name="header">Send now</apex:facet> 
                <apex:inputField value="{!Record.Clone_Send_now__c }"/> 
            </apex:column> 
            
        </apex:pageBlockTable> 
            
            
            
        </apex:pageblock>
    </apex:form>
 
 
</apex:page>

and controller
public class programclone {

    public programclone() {
    
     String myterr = [Select Sales_Territory__c From User Where Id = :UserInfo.getUserId()][0].Sales_Territory__c ;
    System.debug(cloneyear );
        Records =[select Account_Name__r.name,
                         Account_Name__r.billingcity, 
                         Account_Name__r.billingstate, 
                         Account_Name__r.Territory_EWS__c,
                         DIST_EXEC_SPONSOR__r.name,
                         DIST_EXEC_SPONSOR__r.firstname,
                         DIST_E_S_Email__c,
                         Account_Number__c, 
                         Program_Status__c, 
                         RecordTypeid, 
                         Program_Year__c,
                         recordtype.name,
                         CS_YTD_Sales__c,
                         CS_Run_Rate__c,
                         CS_Projected_Payout__c,
                         CS_100PercentCommitted__c,  
                         Clone_Send_now__c , 
                         Clone_no_send__c 
                         from Distributor_Program__c 
                         Where Program_Status__c='active' 
                         and ( CreatedByid = :UserInfo.getUserid()  or   Account_Name__r.Territory_EWS__c = :myterr ) 
                         and Program_Year__c = :cloneyear 
                         and   Clone_no_send__c  != true 
                         and   Clone_Send_now__c != true 
                         order by Account_Name__r.name limit 200]; 


    }


        cloan_program_year__c settings = cloan_program_year__c.getInstance('cloneyear');
   
        string cloneyear =string.valueof(settings.Year__c);

       
   

public List<Distributor_Program__c> Records {get; set;} 


 

    public PageReference cancel() {
             return new PageReference('/home/home.jsp');
    }


    public PageReference Save() {
       update records;
        return new PageReference('/apex/nextstep');

    }



    }

And the test class 
 
@isTest
private class testclonedppage {

private static final Id acctrtid = Schema.SObjectType.account.getRecordTypeInfosByName().get('Distributor').getRecordTypeId();
private static final Id dpid = Schema.SObjectType.Distributor_Program__c.getRecordTypeInfosByName().get('PS Cornerstone').getRecordTypeId();

	@isTest static void test_method_one() {
		 
   

	Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
	User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
	EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
	LocaleSidKey='en_US', ProfileId = p.Id, 
	TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@hammybuiltthis.com',
	Sales_Territory__c = '1');

  	cloan_program_year__C cs = new cloan_program_year__c(name = 'cloneyear' , year__c = '2016', year1__c = '2017' );

  	Account acc = new Account();
  	acc.Name='Abce';
	acc.Type='Distributor';
	acc.recordtypeid = acctrtid;
	acc.Territory_EWS__c = '1';
  	
  	insert acc;

  	Contact c = new Contact();
  	c.Firstname = 'first';
  	c.Lastname = 'last';
  	c.Email = 'email@email.com';
  	c.Accountid = acc.id;


  	insert c;

	System.runAs(u) {

		Distributor_Program__c  dp = new Distributor_Program__c();
		dp.RecordTypeId = dpid;
		dp.Account_Number__c = '1';
		dp.Program_Year__c = cs.year__c;
		dp.Account_Name__c = acc.id;
		dp.DIST_EXEC_SPONSOR__c = c.id;
		insert dp;
		
System.debug('Current Contact name: ' + c.firstname);
System.debug('Current User: ' + UserInfo.getUserName());
System.debug('Current Profile: ' + UserInfo.getProfileId());
		
		Test.setCurrentPage(Page.programclone);
		programclone controller = new programclone();
	
		controller.save();


		

		}

	}
	

	
}

 
Best Answer chosen by MrHammy
Apoorv Saxena 4Apoorv Saxena 4
Hi Hammy,

It seems like you forgot to insert your custom settings record in your test class(line 18).

cloan_program_year__C cs = new cloan_program_year__c(name = 'cloneyear' , year__c ='2016', year1__c = '2017' );
insert cs;

Please mark this question as Solved if this helps you so that others can view it as a proper solution.

Thanks,
Apoorv

All Answers

AdamDawAdamDaw
Where is Program_Status__c being set? I don't see that in the test class. Also, I'd recommend shifting to a @testSetup for creating your data, and moving it out of your @isTest method.
Apoorv Saxena 4Apoorv Saxena 4
Hi Hammy,

It seems like you forgot to insert your custom settings record in your test class(line 18).

cloan_program_year__C cs = new cloan_program_year__c(name = 'cloneyear' , year__c ='2016', year1__c = '2017' );
insert cs;

Please mark this question as Solved if this helps you so that others can view it as a proper solution.

Thanks,
Apoorv
This was selected as the best answer
karthikeyan perumalkarthikeyan perumal
Hello, 

update your apex class with this code.

may be this value is null "string.valueof(settings.Year__c)"
 
public class programclone {

    public programclone() {
    
     String myterr = [Select Sales_Territory__c From User Where Id = :UserInfo.getUserId()][0].Sales_Territory__c ;
    System.debug(cloneyear );
        Records =[select Account_Name__r.name,
                         Account_Name__r.billingcity, 
                         Account_Name__r.billingstate, 
                         Account_Name__r.Territory_EWS__c,
                         DIST_EXEC_SPONSOR__r.name,
                         DIST_EXEC_SPONSOR__r.firstname,
                         DIST_E_S_Email__c,
                         Account_Number__c, 
                         Program_Status__c, 
                         RecordTypeid, 
                         Program_Year__c,
                         recordtype.name,
                         CS_YTD_Sales__c,
                         CS_Run_Rate__c,
                         CS_Projected_Payout__c,
                         CS_100PercentCommitted__c,  
                         Clone_Send_now__c , 
                         Clone_no_send__c 
                         from Distributor_Program__c 
                         Where Program_Status__c='active' 
                         and ( CreatedByid = :UserInfo.getUserid()  or   Account_Name__r.Territory_EWS__c = :myterr ) 
                         and Program_Year__c = :cloneyear 
                         and   Clone_no_send__c  != true 
                         and   Clone_Send_now__c != true 
                         order by Account_Name__r.name limit 200]; 


    }


        cloan_program_year__c settings = cloan_program_year__c.getInstance('cloneyear');
        
		if(settings.Year__c !=null)
		{
        string cloneyear =string.valueof(settings.Year__c);
		}

       
   

    public List<Distributor_Program__c> Records {get; set;} 


 

    public PageReference cancel() {
             return new PageReference('/home/home.jsp');
    }


    public PageReference Save() {
       update records;
        return new PageReference('/apex/nextstep');

    }



    }

Hope this will help you, 

Thanks
karthik