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
joestuartjoestuart 

Error In APEX Test - System.NullPointerException: Attempt to de-reference a null object

Hi,
If someone could please help me fix my test.  I am trying to deploy my code but stuck in the Test with the error: 
System.NullPointerException: Attempt to de-reference a null object
Stack Trace
Class.webBookingEventsCon.processEvent: line 920, column 1
Class.webBookingEventsCon.eventPayNow: line 1019, column 1
Class.webBookingEventsConTest.webBookingEventsConUnitTest: line 152, column 1

Here is my test
/**
 *t
 */
@isTest
private class webBookingEventsConTest {

    static testMethod void webBookingEventsConUnitTest() {
        
        // **** Setup Test Data *****
        
        Product2 product = new Product2( 
            Discounted_Price_Theshold__c=3, 
            Table_Size__c=7, 
            Discounted_Investment_Description__c='A discount of $50pp applies for 3 or more people', 
            Table_Investment_Details__c=3750.00, 
            Discounted_Investment_Details__c=545.00, 
            Standard_Investment_Details__c=595.00, 
            Name='Test Course',
            GST__c=true,
            IsActive=true);
        insert product;
        
        List<Campaign> campaignList = new List<Campaign>();
        campaignList.add(new Campaign (
            Name='Test Event - 30 days out',
            Venue_Name__c='The Venue',
            Address_Details__c='100 Flinders St, Melbourne', 
            StartDate=system.today()+30, 
            Running_Times__c='9am start',
            Event_Type__c = 'Workshop', 
            Product_Name__c=product.Id, 
            Location__c='Melbourne', 
            IsActive=true,
            Opportunity_Category__c='VIC Public Program',
            Publish_on_the_web__c = true));  
        campaignList.add(new Campaign (
            Name='Test Event - 90 days out',
            Venue_Name__c='The Venue',
            Address_Details__c='100 Flinders St, Melbourne', 
            StartDate=system.today()+90, 
            Running_Times__c='9am start', 
            Product_Name__c=product.Id, 
            Location__c='Melbourne', 
            Opportunity_Category__c='VIC Public Program',
            IsActive=true,
            Publish_on_the_web__c = true)); 
        insert campaignList;
        
        
        Account account = new Account(Name='Test Co Pty Ltd');
        insert account;
        
        Contact contact = new Contact(firstName='TestFN',LastName='TestLN',email='testfn.testln@test.com',AccountId=account.Id);
        insert contact;
        
        // ** Start Testing ***/
        PageReference pageRef = new PageReference('/apex/eventsSelectList?prodId='+product.Id);
        Test.setCurrentPage(pageRef);

        webBookingEventsCon controller = new webBookingEventsCon();
        
        system.assertEquals(2, controller.upcommingEvents.size(), 'Check 2 upcomming events found');        
        
        system.assertEquals(product.Id,controller.Product.Id,'check product loaded');
        
        system.assert(controller.noIndividualsList.size()>0,'Load and check Individuals Picklist Ok');    
        
        system.assert(controller.noGroupsList.size()>0,'Load and check Groups Picklist Ok');  

        // >>> Events Select, plus select No. of Attenddee's        
        controller.eventSelectConfirm(); // fails no campaign
        controller.campaignId='000';
        
        controller.eventSelectConfirm(); // fails no Individual or groups selected
        controller.payment.No_Of_Groups__c='2';
        controller.payment.No_Of_Individuals__c='1';
        
        //Added for GST
        controller.payment.total_GST__c ='100';
        
        controller.eventSelectConfirm(); // fails indvalid campaign
        controller.campaignId=campaignList[0].Id;
        
        string nextURL = controller.eventSelectConfirm().getUrl();
        system.assertEquals('/apex/eventsregistration',nextURL,'Should go onto Events Registration entry screen');
        
        // >>> Events Registration   
        Contact primaryContact = controller.primaryContact;  
        primaryContact.firstName='TestFN';
        primaryContact.LastName='TestLN';
        primaryContact.Phone='03 9397 9999';
        primaryContact.Email='testfn.testln@test.com';
        primaryContact.Organisation__c='Test Co Pty Ltd';
   
        controller.primaryContact = primaryContact;         
        controller.IWillAttend = true;
        
        nextURL = controller.eventRegistrationConfirm().getUrl();
        system.assertEquals('/apex/eventsattendees',nextURL,'Should go onto Events Attenddee entry screen');        
        
        // >>> Events Attenddee registration
        controller.attendeeSaveAndAdd();
        system.assertEquals(2, controller.currentNoAttenddee, 'Should have saved first attendde (defaulted) and be waiting for second.');
        
        controller.currAttendeeContact.contact.lastName='G1.1';
        controller.attendeeSaveAndAdd();
        controller.currAttendeeContact.contact.lastName='G1.2';
        controller.attendeeSaveAndAdd();
        controller.currAttendeeContact.contact.lastName='G1.3';
        controller.attendeeSaveAndAdd();
        controller.currAttendeeContact.contact.lastName='G1.4';
        controller.attendeeSaveAndAdd();
        controller.currAttendeeContact.contact.lastName='G1.5';
        controller.attendeeSaveAndAdd();
        controller.currAttendeeContact.contact.lastName='G1.6';
        controller.attendeeSaveAndAdd();
        controller.currAttendeeContact.contact.lastName='G1.7';
        controller.attendeeSaveAndAdd();
        
        controller.currAttendeeContact.contact.lastName='G2.1';
        controller.attendeeSaveAndAdd();
        controller.currAttendeeContact.contact.lastName='G2.2';
        controller.attendeeSaveAndAdd();
        controller.currAttendeeContact.contact.lastName='G2.3';
        controller.attendeeSaveAndAdd();
        controller.currAttendeeContact.contact.lastName='G2.4';
        controller.attendeeSaveAndAdd();
        controller.currAttendeeContact.contact.lastName='G2.5';
        controller.attendeeSaveAndAdd();
        controller.currAttendeeContact.contact.lastName='G2.6';
        controller.attendeeSaveAndAdd();
        
        
        controller.currentNoAttenddee = 2;
        controller.attendeeEdit();
        system.assertEquals(controller.currAttendeeContact.contact.lastName,'G1.1','Check Controller Edit');
        controller.attendeeSaveAndAdd();
        
        controller.currentNoAttenddee = 2;
        controller.attendeeDel();
        system.assertEquals(13,controller.attendeeContacts.size(), 'Check attendee deleted OK');
        
        
        
        controller.eventPayNow(); // should fail, not enough attenddess;
        
        controller.currAttendeeContact.contact.lastName='G2.7';
        controller.attendeeSaveAndAdd();
        
        system.assertEquals('Save',controller.attenddeeSaveMode,'Should be in save mode as now adding last item.');
        controller.currAttendeeContact.contact.lastName='G2.1';
        controller.attendeeSaveAndAdd();
                
        controller.eventPayNow();   
        
        List<CampaignMember> campaignMemberList = new List<CampaignMember>([Select Id from CampaignMember where campaignId = :campaignList[0].Id]);
        System.assertEquals(15,campaignMemberList.size(),'Check details updated - campaign members');
        
        
        // >>> Post payment update.
        AAkPay__Payment_Txn__c paymentTxn = new AAkPay__Payment_Txn__c(AAkPay__Payment_Response_Code__c='00',AAkPay__Campaign__c=campaignList[0].Id,AAkPay__Card_Charged_Amount__c=500,Payment__c=controller.payment.Id);
        insert paymentTxn;
        
        ApexPages.currentPage().getParameters().put('id',paymentTxn.Id);
        controller.eventsCheckOutConfirmInit();
        
        Payment__c payment2 = ([Select Id, Paid_In_Full__c from Payment__c where Id = :controller.payment.Id]);
        system.assertEquals(true,Payment2.Paid_In_Full__c,'Check post payment updates completed OK');
                    
        AAkonsultUtils.errorLog('eventCon.eventsCheckOutConfirmInit','test Subject ','test description',null);                                                  
    }
}

Thank you for any help.
Joe​​
Sampath SuranjiSampath Suranji
Hi,
It seems issue inside the attendeeSaveAndAdd method. It is better if you can provide the webBookingEventsCon class also.
regards
Raj VakatiRaj Vakati
Looks like its a data setup issue in the test class ..whihc is not passing the data to controller 

but for time being .. keep the line in try and catch in test class and deploy it
 
try{        
controller.attendeeSaveAndAdd();
}catch(Exception e){

}