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
Austin_SteveAustin_Steve 

NullPointerException Error on my test class

Recieving the error (NullPointerException) Unable to save resoure(s), when I try to compile the code below. 

 

Any guidance would be greatly appreciated. Thanks in advance.

 

 

public with sharing class TestUpDtCnt {

static testMethod void ActUpdateTest(){
//create the test account
Account a = new Account(Name='My Test Account',
industry='corporate');
insert a;

//create the test contact
Contact c = new Contact(
FirstName='Tester10101',
LastName='Tester10101',
CompanyInterest__c='NetSim',
accountid = a.id);
insert c;

Set<String> accSet = new Set<String>();
List<Contact> cnts = new List<contact>();

for (Account a2 : [select id from Account where name = 'My Test Account'] ){
accSet.add(a2.id);
}

cnts = [Select ID from Contact where accountid in:accSet];

try{
for (Account acct : [select id from Account where id in:accSet]){
for(Contact ct:cnts){
if (acct.Type == 'Customer - Netsim' || acct.Type =='Customer - NetsimOD'){
ct.Customer_Type__c = 'Former';
}
update cnts;
}
}
}catch (System.DmlException e){
System.debug('we caught a dml exception: ' + e.getDmlMessage(0));
}
}
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Jon Mountjoy_Jon Mountjoy_

When exactly do you get the null pointer exception?  Are you using the IDE or the web interface? 

 

I can save that code just fine if I remove the lines with CompanyInterest (I'm guessing that's a custom field you added)

 

 

Regards,
Jon

Regards,Jon

 

All Answers

Jon Mountjoy_Jon Mountjoy_

That's weird - you shouldn't get that error.  I've notified a few folk about that.

 

I do notice a small error in your test code though - this line

   for (Account acct : [select id from Account where id in:accSet

should probably be

   for (Account acct : [select id, type from Account where id in:accSet

(because later on you say acct.type)

 

Jon

Jon Mountjoy_Jon Mountjoy_

When exactly do you get the null pointer exception?  Are you using the IDE or the web interface? 

 

I can save that code just fine if I remove the lines with CompanyInterest (I'm guessing that's a custom field you added)

 

 

Regards,
Jon

Regards,Jon

 

This was selected as the best answer
Austin_SteveAustin_Steve

I added type field in to the SOQL query and it saves across fine.  I was using the IDE.  Thank you for your help.