0

There is an issue with the test class:

Error Message System.NullPointerException: Attempt to de-reference a null object Stack Trace Class.AnalyzeObjectBatch.start: line 30, column 1

global Database.querylocator start(Database.BatchableContext BC) {
    system.debug('>>>>> Batch Start ');
    GlobalDescribeMap = Schema.getGlobalDescribe();

    string fieldStr = '';
    **Schema.DescribeSObjectResult objResult = GlobalDescribeMap.get(objectName).getDescribe();**
    Map<String, Schema.SObjectField> objectFields = objResult.fields.getMap();
    fieldSet = new Set<string>(objectFields.keyset());
    fieldAnalysisList = [select id,name,Populated_On__c,Type__c from Field_Analysis__c where Object_Analysis__c=:objForAnalysisID ];
    for(Field_Analysis__c fldA : fieldAnalysisList){
      if(fieldSet.contains(fldA.name)){
        fieldAnalysisOldMap.put(fldA.name,fldA);
      }else{
        fieldAnalysisListToDel.add(fldA);
      }
    }

Below is the test class I have written

@isTest(seeAllData=false)
public class AnalyzeObjectBatchTests {

    static testMethod void testAuditTrailBatchDeleteTemp() 
    {
        Object_Analysis__c objAna = New Object_Analysis__c();
        objAna.Name = 'TestName';
        objAna.Record_Count__c = 1;
        objAna.Object_Name__c = 'Object Analysis';
        insert objAna;

        Field_Analysis__c fldAna = New Field_Analysis__c();
        fldAna.Name = 'Test';
        fldAna.Object_Analysis__c = objAna.Id;
        insert fldAna;


        Test.StartTest();    
        AnalyzeObjectBatch a = new AnalyzeObjectBatch(objAna.Id);
        Id batchProcessId = Database.executeBatch(a);
        Test.StopTest();
    } 
}
1

Following line is quite unstable in your code:

 Schema.DescribeSObjectResult objResult = GlobalDescribeMap.get(objectName).getDescribe();

You are passing objectname argument. So, in any case if objectname is null or has value which is not a valid sobject API name then you will definitely get the error:

Error Message System.NullPointerException: Attempt to de-reference a null object Stack Trace

So, you must make sure that objectname is valid by adding condition like; if(objectName != null) and if(GlobalDescribeMap.get(objectName) != null)

0

In addition to @Ashwani answer,

global Database.querylocator start(Database.BatchableContext BC) {
system.debug('>>>>> Batch Start ');
GlobalDescribeMap = Schema.getGlobalDescribe();

string fieldStr = '';
Schema.DescribeSObjectResult objResult = GlobalDescribeMap.get(objectName).getDescribe();

The last line presumes objectName is a variable known to this class. Since this is a start method, it can only come from either

a) A value passed to the batch class's constructor

b) A static variable in the batch class

c) A value set on the class's instance before calling Database.executeBatch(..)

It looks like you are trying to do a) with

AnalyzeObjectBatch a = new AnalyzeObjectBatch(objAna.Id);

in your testmethod. But here you are passing an ID, not an objectname.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.