You need to sign in to do that
Don't have an account?

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));
}
}
}
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
All Answers
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
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
I added type field in to the SOQL query and it saves across fine. I was using the IDE. Thank you for your help.