I am receiving an Error in my Test class, but am unsure of the reason why. Here is the Error:
Exception: Update failed. First exception on row 0 with id a0i4C000000HfieQAC; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TABLoan_beforeupdate: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object
Class.TestTABLoan_beforeupdate.testTABbeforeupdate: line 30, column 1
Trigger.TABLoan_beforeupdate: line 9, column 1: []
Below is the Test Class:
@isTest
public class TestTABLoan_beforeupdate {
static testMethod void testTABbeforeupdate() {
//Create records to test
Account myAccount = new Account();
myAccount.Name = 'My Awesome Account';
myAccount.Industry = 'Energy';
myAccount.Type = 'Association';
myAccount.RecordTypeId = [Select Id,SobjectType,Name From RecordType WHERE Name ='Business' and SobjectType ='Account' limit 1].Id;
insert myAccount;
Opportunity myopportunity = new Opportunity();
myopportunity.Name = 'My Opportunity ties to a lead.';
myopportunity.Account = myAccount;
myopportunity.StageName = 'Applicant';
myopportunity.CloseDate = Date.today();
insert myopportunity;
LLC_BI__Loan__c myLoan = new LLC_BI__Loan__c();
myLoan.Name = 'This Lead has an Opportunity,';
myLoan.Opportunity_Source__c = myopportunity.Id;
myLoan.LLC_BI__Principal_Balance__c = 3000000;
myLoan.LLC_BI__Account__c = myAccount.Id;
myLoan.LLC_BI__Product_Line__c = 'ABL';
myLoan.LLC_BI__Product_Type__c = 'ABL';
myLoan.LLC_BI__Product__c = 'Other';
insert myLoan;
myLoan.LLC_BI__Principal_Balance__c = 250000;
update myLoan;
myopportunity = [SELECT Id, PrincipalBalance__c From Opportunity LIMIT 1];
System.assertEquals(myopportunity.PrincipalBalance__c, 250000);
}
}
And here is the trigger I am testing:
trigger TABLoan_beforeupdate on LLC_BI__Loan__c (before update) {
List<LLC_BI__Loan__c> loans = new List<LLC_BI__Loan__c>();
for (LLC_BI__Loan__c l : Trigger.new){
// Access the "old" record by its ID in Trigger.oldMap
LLC_BI__Loan__c oldl = Trigger.oldMap.get(l.id);
// Check to see if loan has principal balance and Opportunity source
if ((oldl.LLC_BI__Principal_Balance__c != l.LLC_BI__Principal_Balance__c) && (l.Opportunity_Source__c != null )) {
l.Opportunity_Source__r.PrincipalBalance__c = l.LLC_BI__Principal_Balance__c;
loans.add(l);
}
}
update loans;
}