0

Why do i get this error:

System.NullPointerException: Attempt to de-reference a null object

My testclass looks like:

    @isTest
public class attachPDFTestClass { 

  static Account testAccount;
  static {
    testAccount = new Account();
    testAccount.Name = 'REALIANCE';
    insert testAccount;
  }  

    static testMethod void testAttachments() { 
 system.debug('line 2'); 
    //system.debug('id Testaccount: '+testAccount.id);
 system.debug(testAccount.Id); 

    Facturatie__c testFactuur = new Facturatie__c();
    system.debug('line 3'); 
    system.debug(testAccount.Name);
    testFactuur.Accountname__r.Name = testAccount.Name;  
 system.debug('line 4');
    insert testFactuur;
 system.debug('id testFactuur: '+testFactuur.id);


system.debug(testAccount.Name);  is ok
2
  • which line throws the exception? Commented Feb 16, 2017 at 16:36
  • on line 12 @isTest public class attachPDFTestClass { static testMethod void testAttachments() { Account testAccount = new Account(); testAccount.Name = 'REALIANCE'; insert testAccount; system.debug(testAccount.Id); Facturatie__c currentRecord = new Facturatie__c(); currentRecord.Accountname__r.Name = testAccount.Name; system.debug('testAccount debug'+ testAccount.Name); insert currentRecord; Commented Feb 16, 2017 at 17:37

1 Answer 1

1

You can't insert your Account outside your test method and access it directly.

You have two solutions:

  • Add a test setup method which inserts the account. Then request this account from your test method
  • Insert your account at the start of your test method like this:

static testMethod void testAttachments() { 
    Account testAccount = new Account();
    testAccount.Name = 'REALIANCE';
    insert testAccount;

 system.debug('line 2'); 
    //system.debug('id Testaccount: '+testAccount.id);
 system.debug(testAccount.Id); 

    Facturatie__c testFactuur = new Facturatie__c();
    system.debug('line 3'); 
    system.debug(testAccount.Name);
    testFactuur.Accountname__c = testAccount.Id;  
 system.debug('line 4');
    insert testFactuur;
 system.debug('id testFactuur: '+testFactuur.id);


system.debug(testAccount.Name);  is ok
3
  • The system.debug(testAccount.Name); is ok, so this part works fine. The error comes from the testFactuur i think as system.debug('line 3'); system.debug(testAccount.Name); both work except for system.debug('line 4'); Commented Feb 16, 2017 at 16:43
  • I updated my answer. Commented Feb 16, 2017 at 16:47
  • Still get same error on line 12: @isTest public class attachPDFTestClass { static testMethod void testAttachments() { Account testAccount = new Account(); testAccount.Name = 'REALIANCE'; insert testAccount; system.debug(testAccount.Id); Facturatie__c currentRecord = new Facturatie__c(); currentRecord.Accountname__r.Name = testAccount.Name; system.debug('testAccount debug'+ testAccount.Name); insert currentRecord; Commented Feb 16, 2017 at 17:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .