You need to sign in to do that
Don't have an account?
Anna Suganthi
System.NullPointerException: Attempt to de-reference a null object - how to solve?
Hello everyone, i have the following trigger test class. When i run the test I get the System.NullPointerException: Attempt to de-reference a null object error in line 59,
@isTest
private class accountNameDupePreventer {
static testMethod void myUnitTest() {
// TO DO: implement unit test
Test.startTest();
// First make sure there are no accounts already in the system
// that have the Accounts used for testing
Set<String> testAccountNames = new Set<String>();
testAccountNames.add('Test1Dup Inc.');
testAccountNames.add('Test2Dup Inc.');
testAccountNames.add('Test3Dup Inc.');
System.assert([SELECT count() FROM Account WHERE Name IN :testAccountNames] == 0);
// Seed the database with some accounts, and make sure they can be bulk inserted successfully.
Account account1 = new Account(Name = 'Test1Dup Inc.', BillingCountry = 'United States', BillingState = 'Texas');
Account account2 = new Account(Name = 'Test4Dup Inc.', BillingCountry = 'United States', BillingState = 'California');
Account account3 = new Account(Name = 'Test5Dup Inc.', BillingCountry = 'United States', BillingState = 'Florida');
Account[] accts = new Account[] { account1, account2, account3 };
insert accts;
// Now make sure that some of these accounts can be changed and
// then bulk updated successfully. Note that account1 is not
// being changed, but is still being passed to the update
// call. This should be OK.
account2.Name = 'Test2Dup Inc.';
account3.Name = 'Test3Dup Inc.';
update accts;
// Make sure that single row account duplication prevention works on insert.
Account dup1 = new Account(Name='Test1Dup Inc.', BillingCountry = 'United States');
try {
insert dup1;
System.assert(false);
} catch (DmlException e) {
System.assert(e.getNumDml() == 1);
System.assert(e.getDmlIndex(0) == 0);
System.assert(e.getDmlFields(0).size() == 1);
System.assert(e.getDmlFields(0)[0] == Account.Name);
System.assert(e.getDmlMessage(0).indexOf(
'An account with this account\'s name already exists.') > -1);
}
// Make sure that single row account duplication prevention works on update.
dup1 = new Account(Id=account1.Id, Name='Test3Dup Inc.', BillingCountry = 'United States');
try {
update dup1;
System.assert(false);
} catch (DmlException e) {
System.assert(e.getNumDml() == 1);
System.assert(e.getDmlIndex(0) == 0);
System.assert(e.getDmlFields(0).size() == 1);
System.assert(e.getDmlFields(0)[0] == Account.Name);
System.assert(e.getDmlMessage(0).indexOf(
'An account with this account\'s name already exists.') > -1);
}
// Make sure that bulk account duplication prevention works on
// insert. Note that the first item being inserted is fine,
// but the second and third items are duplicates. Note also
// that since at least one record insert fails, the entire
// transaction will be rolled back.
dup1 = new Account(Name='Test4Dup Inc.', BillingCountry = 'United States');
Account dup2 = new Account(Name='Test2Dup Inc.', BillingCountry = 'United States');
Account dup3 = new Account(Name='Test3Dup Inc.', BillingCountry = 'United States');
Account[] dups = new Account[] {dup1, dup2, dup3};
try {
insert dups;
System.assert(false);
} catch (DmlException e) {
// Num=2 because dup1 not fails
System.assert(e.getNumDml() == 2);
// dup2 fails
System.assert(e.getDmlIndex(0) == 1);
System.assert(e.getDmlFields(0).size() == 1);
System.assert(e.getDmlFields(0)[0] == Account.Name);
System.assert(e.getDmlMessage(0).indexOf(
'An account with this account\'s name already exists.') > -1);
// dup3 fails
System.assert(e.getDmlIndex(1) == 2);
System.assert(e.getDmlFields(1).size() == 1);
System.assert(e.getDmlFields(1)[0] == Account.Name);
System.assert(e.getDmlMessage(1).indexOf(
'An account with this account\'s name already exists.') > -1);
}
// Make sure that bulk account duplication prevention works on
// update. Note that the first item being updated is fine,
// because the name is new, and the second item is
// also fine, but in this case it's because the
// name doesn't change. The third case is flagged as an
// error because it is a duplicate of the name of the
// first account's value in the database, even though that value
// is changing in this same update call. It would be an
// interesting exercise to rewrite the trigger to allow this
// case. Note also that since at least one record update
// fails, the entire transaction will be rolled back.
dup1 = new Account(Id=account1.id, Name='Test5Dup Inc.', BillingCountry = 'United States');
dup2 = new Account(Id=account2.id, Name='Test2Dup Inc.', BillingCountry = 'United States');
dup3 = new Account(Id=account3.id, Name='Test1Dup Inc.', BillingCountry = 'United States');
dups = new Account[] {dup1, dup2, dup3};
try {
update dups;
System.assert(false);
} catch (DmlException e) {
System.debug(e.getNumDml());
System.debug(e.getDmlMessage(0));
System.assert(e.getNumDml() == 1);
System.assert(e.getDmlIndex(0) == 2);
System.assert(e.getDmlFields(0).size() == 1);
System.assert(e.getDmlFields(0)[0] == Account.Name);
System.assert(e.getDmlMessage(0).indexOf(
'An account with this account\'s name already exists.') > -1);
}
// Make sure that duplicates in the submission are caught when
// inserting accounts. Note that this test also catches an
// attempt to insert an account where there is an existing
// duplicate.
dup1 = new Account(Name='Test4Dup Inc.', BillingCountry = 'United States');
dup2 = new Account(Name='Test4Dup Inc.', BillingCountry = 'United States');
dup3 = new Account(Name='Test3Dup Inc.', BillingCountry = 'United States');
dups = new Account[] {dup1, dup2, dup3};
try {
insert dups;
System.assert(false);
} catch (DmlException e) {
System.assert(e.getNumDml() == 2);
System.assert(e.getDmlIndex(0) == 1);
System.assert(e.getDmlFields(0).size() == 1);
System.assert(e.getDmlFields(0)[0] == Account.Name);
System.assert(e.getDmlMessage(0).indexOf(
'Another inserted/updated account from bulk operation has the same name.') > -1);
System.assert(e.getDmlIndex(1) == 2);
System.assert(e.getDmlFields(1).size() == 1);
System.assert(e.getDmlFields(1)[0] == Account.Name);
System.assert(e.getDmlMessage(1).indexOf(
'An account with this account\'s name already exists.') > -1);
}
// Make sure that duplicates in the submission are caught when
// updating accounts. Note that this test also catches an attempt
// to update an account where there is an existing duplicate.
dup1 = new Account(Id=account1.id, Name='Test4Dup Inc.', BillingCountry = 'United States');
dup2 = new Account(Id=account2.id, Name='Test4Dup Inc.', BillingCountry = 'United States');
dup3 = new Account(Id=account3.id, Name='Test2Dup Inc.', BillingCountry = 'United States');
dups = new Account[] {dup1, dup2, dup3};
try {
update dups;
System.assert(false);
} catch (DmlException e) {
System.assert(e.getNumDml() == 2);
System.assert(e.getDmlIndex(0) == 1);
System.assert(e.getDmlFields(0).size() == 1);
System.assert(e.getDmlFields(0)[0] == Account.Name);
System.assert(e.getDmlMessage(0).indexOf(
'Another inserted/updated account from bulk operation has the same name.') > -1);
System.assert(e.getDmlIndex(1) == 2);
System.assert(e.getDmlFields(1).size() == 1);
System.assert(e.getDmlFields(1)[0] == Account.Name);
System.assert(e.getDmlMessage(1).indexOf(
'An account with this account\'s name already exists.') > -1);
}
Test.stopTest();
}
}
"System.assert(e.getDmlFields(0).size() == 1);", the error is thrown in this line. Can anyone tell me how to solve this?
ThanksinAdvance!
@isTest
private class accountNameDupePreventer {
static testMethod void myUnitTest() {
// TO DO: implement unit test
Test.startTest();
// First make sure there are no accounts already in the system
// that have the Accounts used for testing
Set<String> testAccountNames = new Set<String>();
testAccountNames.add('Test1Dup Inc.');
testAccountNames.add('Test2Dup Inc.');
testAccountNames.add('Test3Dup Inc.');
System.assert([SELECT count() FROM Account WHERE Name IN :testAccountNames] == 0);
// Seed the database with some accounts, and make sure they can be bulk inserted successfully.
Account account1 = new Account(Name = 'Test1Dup Inc.', BillingCountry = 'United States', BillingState = 'Texas');
Account account2 = new Account(Name = 'Test4Dup Inc.', BillingCountry = 'United States', BillingState = 'California');
Account account3 = new Account(Name = 'Test5Dup Inc.', BillingCountry = 'United States', BillingState = 'Florida');
Account[] accts = new Account[] { account1, account2, account3 };
insert accts;
// Now make sure that some of these accounts can be changed and
// then bulk updated successfully. Note that account1 is not
// being changed, but is still being passed to the update
// call. This should be OK.
account2.Name = 'Test2Dup Inc.';
account3.Name = 'Test3Dup Inc.';
update accts;
// Make sure that single row account duplication prevention works on insert.
Account dup1 = new Account(Name='Test1Dup Inc.', BillingCountry = 'United States');
try {
insert dup1;
System.assert(false);
} catch (DmlException e) {
System.assert(e.getNumDml() == 1);
System.assert(e.getDmlIndex(0) == 0);
System.assert(e.getDmlFields(0).size() == 1);
System.assert(e.getDmlFields(0)[0] == Account.Name);
System.assert(e.getDmlMessage(0).indexOf(
'An account with this account\'s name already exists.') > -1);
}
// Make sure that single row account duplication prevention works on update.
dup1 = new Account(Id=account1.Id, Name='Test3Dup Inc.', BillingCountry = 'United States');
try {
update dup1;
System.assert(false);
} catch (DmlException e) {
System.assert(e.getNumDml() == 1);
System.assert(e.getDmlIndex(0) == 0);
System.assert(e.getDmlFields(0).size() == 1);
System.assert(e.getDmlFields(0)[0] == Account.Name);
System.assert(e.getDmlMessage(0).indexOf(
'An account with this account\'s name already exists.') > -1);
}
// Make sure that bulk account duplication prevention works on
// insert. Note that the first item being inserted is fine,
// but the second and third items are duplicates. Note also
// that since at least one record insert fails, the entire
// transaction will be rolled back.
dup1 = new Account(Name='Test4Dup Inc.', BillingCountry = 'United States');
Account dup2 = new Account(Name='Test2Dup Inc.', BillingCountry = 'United States');
Account dup3 = new Account(Name='Test3Dup Inc.', BillingCountry = 'United States');
Account[] dups = new Account[] {dup1, dup2, dup3};
try {
insert dups;
System.assert(false);
} catch (DmlException e) {
// Num=2 because dup1 not fails
System.assert(e.getNumDml() == 2);
// dup2 fails
System.assert(e.getDmlIndex(0) == 1);
System.assert(e.getDmlFields(0).size() == 1);
System.assert(e.getDmlFields(0)[0] == Account.Name);
System.assert(e.getDmlMessage(0).indexOf(
'An account with this account\'s name already exists.') > -1);
// dup3 fails
System.assert(e.getDmlIndex(1) == 2);
System.assert(e.getDmlFields(1).size() == 1);
System.assert(e.getDmlFields(1)[0] == Account.Name);
System.assert(e.getDmlMessage(1).indexOf(
'An account with this account\'s name already exists.') > -1);
}
// Make sure that bulk account duplication prevention works on
// update. Note that the first item being updated is fine,
// because the name is new, and the second item is
// also fine, but in this case it's because the
// name doesn't change. The third case is flagged as an
// error because it is a duplicate of the name of the
// first account's value in the database, even though that value
// is changing in this same update call. It would be an
// interesting exercise to rewrite the trigger to allow this
// case. Note also that since at least one record update
// fails, the entire transaction will be rolled back.
dup1 = new Account(Id=account1.id, Name='Test5Dup Inc.', BillingCountry = 'United States');
dup2 = new Account(Id=account2.id, Name='Test2Dup Inc.', BillingCountry = 'United States');
dup3 = new Account(Id=account3.id, Name='Test1Dup Inc.', BillingCountry = 'United States');
dups = new Account[] {dup1, dup2, dup3};
try {
update dups;
System.assert(false);
} catch (DmlException e) {
System.debug(e.getNumDml());
System.debug(e.getDmlMessage(0));
System.assert(e.getNumDml() == 1);
System.assert(e.getDmlIndex(0) == 2);
System.assert(e.getDmlFields(0).size() == 1);
System.assert(e.getDmlFields(0)[0] == Account.Name);
System.assert(e.getDmlMessage(0).indexOf(
'An account with this account\'s name already exists.') > -1);
}
// Make sure that duplicates in the submission are caught when
// inserting accounts. Note that this test also catches an
// attempt to insert an account where there is an existing
// duplicate.
dup1 = new Account(Name='Test4Dup Inc.', BillingCountry = 'United States');
dup2 = new Account(Name='Test4Dup Inc.', BillingCountry = 'United States');
dup3 = new Account(Name='Test3Dup Inc.', BillingCountry = 'United States');
dups = new Account[] {dup1, dup2, dup3};
try {
insert dups;
System.assert(false);
} catch (DmlException e) {
System.assert(e.getNumDml() == 2);
System.assert(e.getDmlIndex(0) == 1);
System.assert(e.getDmlFields(0).size() == 1);
System.assert(e.getDmlFields(0)[0] == Account.Name);
System.assert(e.getDmlMessage(0).indexOf(
'Another inserted/updated account from bulk operation has the same name.') > -1);
System.assert(e.getDmlIndex(1) == 2);
System.assert(e.getDmlFields(1).size() == 1);
System.assert(e.getDmlFields(1)[0] == Account.Name);
System.assert(e.getDmlMessage(1).indexOf(
'An account with this account\'s name already exists.') > -1);
}
// Make sure that duplicates in the submission are caught when
// updating accounts. Note that this test also catches an attempt
// to update an account where there is an existing duplicate.
dup1 = new Account(Id=account1.id, Name='Test4Dup Inc.', BillingCountry = 'United States');
dup2 = new Account(Id=account2.id, Name='Test4Dup Inc.', BillingCountry = 'United States');
dup3 = new Account(Id=account3.id, Name='Test2Dup Inc.', BillingCountry = 'United States');
dups = new Account[] {dup1, dup2, dup3};
try {
update dups;
System.assert(false);
} catch (DmlException e) {
System.assert(e.getNumDml() == 2);
System.assert(e.getDmlIndex(0) == 1);
System.assert(e.getDmlFields(0).size() == 1);
System.assert(e.getDmlFields(0)[0] == Account.Name);
System.assert(e.getDmlMessage(0).indexOf(
'Another inserted/updated account from bulk operation has the same name.') > -1);
System.assert(e.getDmlIndex(1) == 2);
System.assert(e.getDmlFields(1).size() == 1);
System.assert(e.getDmlFields(1)[0] == Account.Name);
System.assert(e.getDmlMessage(1).indexOf(
'An account with this account\'s name already exists.') > -1);
}
Test.stopTest();
}
}
"System.assert(e.getDmlFields(0).size() == 1);", the error is thrown in this line. Can anyone tell me how to solve this?
ThanksinAdvance!
Check whether the list is null before accessing,
regards