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
Peter Martensen 8Peter Martensen 8 

Can someone help me find the object I haven't referenced properly in the Test Class?

I'm not a developer, so I don't really know what I'm doing, and this code is ugly.  When I run the Test Class, it says that I'm trying to de-reference a null object.  Can someone tell me what object hasn't been referenced?  The fatal error is at line 84 (I think).  This is from the failed Test: 
 Class.attachEmailToMIReports.handleInboundEmail: line 8, column 1
Class.attachEmailToMIReports_Test.testAttachEmail: line 84, column 1
Thanks!
@isTest(seeAllData=false)
public class attachEmailToMIReports_Test {
    //class level variables
    Static Account thisAccount;
    Static Contact thisContact;
    Static Opportunity thisOpportunity;
    Static Market_Intelligence_Reports__c thisMIReports;
    Static Messaging.InboundEmail email;
    Static Messaging.InboundEnvelope env;
    Static Messaging.InboundEmailResult result;
    
           
    //method to set all of the object values needed for testing.
    //This would be where you would call a test factory
    //to set values for the objects need for the test
    //for instance "thisContact = testFactory.InsertAccount();"
    //testFactory being a different test class that has
    //a function InsertAccount() that inserts an account object and returns it.
    static void init() {
        // Create a new email and envelope object.      
		//Email and envelope objects 
		Messaging.InboundEmail email = new Messaging.InboundEmail() ; 
		Messaging.InboundEnvelope env = new Messaging.InboundEnvelope(); 

       
        // Create Test record.
        thisAccount = new Account(Name = 'Test');
        insert thisAccount;
        system.debug('Account Id '+thisAccount.Id);
        thisContact = new Contact(firstName='john',
                                  lastName='smith',
                                  Email='test@test.com',
                                  AccountId = thisAccount.Id);
        insert thisContact ;
        system.debug('Contact Id '+thisContact.Id);
        thisOpportunity = new Opportunity(Name = 'TestOpp',
                                         AccountId = thisAccount.Id,
                                         ContactId = thisContact.Id,
                                         StageName = 'Qualification',
                                         CloseDate = (System.today()+5),
        								 Line_Of_Service__c = 'Agawam - Analytical',
        								 CurrencyIsoCode = 'USD',
                                         Compound_Name__c = 'TestOpp',
                                         New_Compound_For_Cambrex__c = 'Yes',
                                         Opportunity_Date__c = System.today(),
                                         LeadSource = 'Webinar',
                                         Proposal_Due_Date__c = System.today(),
                                         Description = 'TestOpp',
                                         Development_Phase__c = 'Discovery');								 
        insert thisOpportunity;
        system.debug('Opportunity Id '+thisOpportunity.Id);
        thisMIReports = new Market_Intelligence_Reports__c(Name = 'Market Intelligence Report for '+Opportunity.Name,
                                                           Account__c = thisAccount.Id,
            											   Opportunity__c = thisOpportunity.Id);
        insert thisMIReports;
        system.debug('MI Reports '+thisMIReports.Id);
        // Test email settings.
			//Simulate data for the email 
			EmailMessage	newEmail = new EmailMessage(
			Subject = 'My Subject', 
			FromAddress = 'emailaddress@address.com', 
			TextBody = 'My email body'); 

            insert newEmail;
            System.debug('Created Email '+newEmail.Id);
        
			//Simulate binary attachment 
			Messaging.InboundEmail.BinaryAttachment attachment = new 
			Messaging.InboundEmail.BinaryAttachment(); 
			attachment.body = blob.valueOf('my attachment text'); 
			attachment.fileName = 'file.txt'; 
			attachment.mimeTypeSubType = 'text/plain'; 
			email.binaryAttachments = new Messaging.inboundEmail.BinaryAttachment[] { attachment }; 
        
    }    
    //this is the method that test the email handler
    static testMethod void testAttachEmail()
    {
        //sets up the variables to be tested
	init();
        Test.startTest();
            attachEmailToMIReports testInbound = new attachEmailToMIReports();
        	testInbound.handleInboundEmail(email, env);
            //result is the return value of the function
            //result = testInbound.handleInboundEmail(email, env );
            //check to see that the result is not null
            System.assert(result != null);
        	//System.assertNotEquals(null,b);
        Test.stopTest();                    
    }
}

 
Dushyant SonwarDushyant Sonwar
Please post your attachEmailToMIReports class ,  to understand about the issue. This will give more info about the issue.