You need to sign in to do that
Don't have an account?
AL
Apex Test Class Code Coverage Error
Hello all,
I am trying to correct an error in my test class which states 'System.NullPointerException: Attempt to de-reference a null object'. The two lines where I'm receiving error are in the test class - AccountActiveProducts.updateAccounts(listAssets); and in the class
List<Account> accountsToUpdate = new List<Account>();
Which object isn't being instantiated? Below are the classes. Thank you for your help.
Test Class
@isTest
public class AccountActiveProductsTest {
@isTest static void RollupAsset(){
List<Asset> listAssets = new List<Asset>();
Set<String> assetSet = new Set<String>();
Account aTest = new Account(name='Test Acct', billingcity='test', billingstate='Maryland');
insert aTest;
//create asset
Asset asset = new Asset(name = 'Asset 1', AccountId = aTest.Id, ProductFamily__c = 'Bed Stat', Status = 'Shipped' );
insert asset;
List<Account> listAccounts = new List<Account>();
List<Account> listAccountstoUpdate = new List<Account>();
Integer numAssets;
for(Integer i=0; i<200; i++){
Asset ast = new Asset(Name = 'Test Asset' + i, AccountId = aTest.Id, ProductFamily__c = 'Bed Stat', Status = 'Shipped');
if(ast.Status != 'Retired' || ast.Status != 'Obsolete'){
assetSet.add(ast.ProductFamily__c);
}
listAssets.add(ast);
}
insert listAssets;
Set<Id> ParentIds = new Set<Id>();
ParentIds.add(asset.AccountId);
assetSet.add(asset.ProductFamily__c);
listAccounts.add(aTest);
Map<Id, Account> mapAccount = new Map<Id, Account>([Select Id, ProductsOwned__c, (Select Id, ProductFamily__c, Status from Assets Where (Status != 'Retired' OR Status != 'Obsolete' OR Status != null)) From Account Where Id In: ParentIds]);
List<String> assetList = new List<String>();
assetList.addAll(assetSet);
test.startTest();
AccountActiveProducts aap = new AccountActiveProducts();
listAssets.add(asset);
asset.ProductFamily__c = 'Communication Director';
String productFamily = string.join(assetList,', ' );
if(aTest.ProductsOwned__c != productFamily){
aTest.ProductsOwned__c = productFamily;
listAccountstoUpdate.add(aTest);
}
if(!listAccountstoUpdate.isEmpty()){
upsert listAccountstoUpdate;
}
if(listAssets.size() > 0){
update listAssets;
AccountActiveProducts.updateAccounts(listAssets);
}
upsert listAccounts;
test.stopTest();
}
}
Class
public class AccountActiveProducts {
public static void updateAccounts(List<Asset> assetLists){
Set<String> setAssetsToRemove = new Set<String>();
List<String> listAssetsToRemove = new List<String>();
Set<Id> ParentIds = new Set<Id>();
for(Asset asset:assetLists){
ParentIds.add(asset.AccountId);
}
Map<Id, Account> mapAccount = new Map<Id, Account>([Select Id, ProductsOwned__c, (Select Id, ProductFamily__c, Status from Assets Where (Status != 'Retired' OR Status != 'Obsolete' OR Status != null)) From Account Where Id In: ParentIds]);
List<Account> accountsToUpdate = new List<Account>();
if(Trigger.isInsert || Trigger.isUpdate){
for(Account ac : mapAccount.values()){
List<String> assetList = new List<String>();
Set<String> assetSet = new Set<String>();
for(Asset accountAsset: ac.assets){
if(accountAsset.Status != 'Retired' || accountAsset.Status != 'Obsolete' ){
assetSet.add(accountAsset.ProductFamily__c);
}
if(accountAsset.Status == 'Retired' || accountAsset.Status == 'Obsolete'){
assetSet.remove(accountAsset.ProductFamily__c);
}
}
assetList.addAll(assetSet);
assetList.sort();
String productFamily = string.join(assetList,', ' );
if(ac.ProductsOwned__c != productFamily){
ac.ProductsOwned__c = productFamily;
accountsToUpdate.add(ac);
}
}
//End of for loop above
if(!accountsToUpdate.isEmpty()){
upsert accountsToUpdate;
}
}
}
}
I am trying to correct an error in my test class which states 'System.NullPointerException: Attempt to de-reference a null object'. The two lines where I'm receiving error are in the test class - AccountActiveProducts.updateAccounts(listAssets); and in the class
List<Account> accountsToUpdate = new List<Account>();
Which object isn't being instantiated? Below are the classes. Thank you for your help.
Test Class
@isTest
public class AccountActiveProductsTest {
@isTest static void RollupAsset(){
List<Asset> listAssets = new List<Asset>();
Set<String> assetSet = new Set<String>();
Account aTest = new Account(name='Test Acct', billingcity='test', billingstate='Maryland');
insert aTest;
//create asset
Asset asset = new Asset(name = 'Asset 1', AccountId = aTest.Id, ProductFamily__c = 'Bed Stat', Status = 'Shipped' );
insert asset;
List<Account> listAccounts = new List<Account>();
List<Account> listAccountstoUpdate = new List<Account>();
Integer numAssets;
for(Integer i=0; i<200; i++){
Asset ast = new Asset(Name = 'Test Asset' + i, AccountId = aTest.Id, ProductFamily__c = 'Bed Stat', Status = 'Shipped');
if(ast.Status != 'Retired' || ast.Status != 'Obsolete'){
assetSet.add(ast.ProductFamily__c);
}
listAssets.add(ast);
}
insert listAssets;
Set<Id> ParentIds = new Set<Id>();
ParentIds.add(asset.AccountId);
assetSet.add(asset.ProductFamily__c);
listAccounts.add(aTest);
Map<Id, Account> mapAccount = new Map<Id, Account>([Select Id, ProductsOwned__c, (Select Id, ProductFamily__c, Status from Assets Where (Status != 'Retired' OR Status != 'Obsolete' OR Status != null)) From Account Where Id In: ParentIds]);
List<String> assetList = new List<String>();
assetList.addAll(assetSet);
test.startTest();
AccountActiveProducts aap = new AccountActiveProducts();
listAssets.add(asset);
asset.ProductFamily__c = 'Communication Director';
String productFamily = string.join(assetList,', ' );
if(aTest.ProductsOwned__c != productFamily){
aTest.ProductsOwned__c = productFamily;
listAccountstoUpdate.add(aTest);
}
if(!listAccountstoUpdate.isEmpty()){
upsert listAccountstoUpdate;
}
if(listAssets.size() > 0){
update listAssets;
AccountActiveProducts.updateAccounts(listAssets);
}
upsert listAccounts;
test.stopTest();
}
}
Class
public class AccountActiveProducts {
public static void updateAccounts(List<Asset> assetLists){
Set<String> setAssetsToRemove = new Set<String>();
List<String> listAssetsToRemove = new List<String>();
Set<Id> ParentIds = new Set<Id>();
for(Asset asset:assetLists){
ParentIds.add(asset.AccountId);
}
Map<Id, Account> mapAccount = new Map<Id, Account>([Select Id, ProductsOwned__c, (Select Id, ProductFamily__c, Status from Assets Where (Status != 'Retired' OR Status != 'Obsolete' OR Status != null)) From Account Where Id In: ParentIds]);
List<Account> accountsToUpdate = new List<Account>();
if(Trigger.isInsert || Trigger.isUpdate){
for(Account ac : mapAccount.values()){
List<String> assetList = new List<String>();
Set<String> assetSet = new Set<String>();
for(Asset accountAsset: ac.assets){
if(accountAsset.Status != 'Retired' || accountAsset.Status != 'Obsolete' ){
assetSet.add(accountAsset.ProductFamily__c);
}
if(accountAsset.Status == 'Retired' || accountAsset.Status == 'Obsolete'){
assetSet.remove(accountAsset.ProductFamily__c);
}
}
assetList.addAll(assetSet);
assetList.sort();
String productFamily = string.join(assetList,', ' );
if(ac.ProductsOwned__c != productFamily){
ac.ProductsOwned__c = productFamily;
accountsToUpdate.add(ac);
}
}
//End of for loop above
if(!accountsToUpdate.isEmpty()){
upsert accountsToUpdate;
}
}
}
}
jigarshah
Can you please format the posted code using < > utility in the post editor? It would ease the community's overhead of udnerstanding your code and help you with a possible solution.
AL