Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have 2000 batches to run in a single batch job. One of the batch gives "Attempt to de-reference a null object" error. I want to track this error. How can I track? I have used debug logs but they are pretty much in quantity. Is there any way to do so?

share|improve this question
Use try catch for the DML statement. If it fails in any block, compose an email in the catch block and send it to the desired recipient. – Priyanka Nov 27 '12 at 9:29
My code is in production now. I am debugging over there. Can't change my code to reflect changes suggested by you. – doga Nov 27 '12 at 9:57
@doga, since you're not able to modify your code, are there any subsequent queries that run within your execute method so that you can at least get an idea where the code is failing? With 2000 records in a batch, there is likely to be a lot to sift through in your logs. Sometimes, hits to the database is the only useful way of finding where you are. – Phil Hawthorn Nov 27 '12 at 11:45
up vote 2 down vote accepted

I would suggest that you need to add some additional error handling to your batch job. This error message will be caused by you trying to reference a variable that is null, you would need to defensively code for this.

One thing you can do, is to add the Database.Stateful interface to your batch apex class method declaration, e.g.

global class InvoicePosterBatch implements Database.Batchable<SObject>, Database.Stateful

By adding this, you can maintain some kind of log of what is happening - you have to be careful though not to exceed the heap size. One strategy I sometimes employ for circumventing that is something like this:

    if( Limits.getHeapSize()*1.1 >= Limits.getLimitHeapSize() )
        failures = new List<String>();

Debugging the batch 'chunks' is difficult because not only do you have your chunk log, you then may have up to 2000 iterations of records within that log.

Another thing you could do, is say reduce your batch size to 1 and then you will see which record is failing, you could do that by querying the AsyncApexJob object, e.g.

Select Status, NumberOfErrors, ExtendedStatus 
From AsyncApexJob
Where ParentJobId = '707U0000008qTH7'
and NumberOfErrors > 0

You can retrieve the 'ParentJobId' from Setup->Monitor->Apex Jobs

share|improve this answer

If you had debug logging enabled - just query the table that contains them:

select Id, Application, DurationMilliseconds, LastModifiedDate, Location, LogLength, LogUserId, Operation, Request, StartTime, Status, SystemModstamp 
from ApexLog
where status = 'Attempt to de-reference a null object'

(of course fine-tune the query to your user, time criteria etc as you see fit)

Then you can view the log by visiting link like that:

(again - provide your own SF instance and log_id)

EDIT (to answer the comment)

Yeah, I'm positive Status-based search can be the way to dig it out.

select Count(Id), Status
from ApexLog
group by status
order by Count(Id) DESC

results from my prod. org.

share|improve this answer
When I am writing the query it return no records. I believe status can only be success or error? – doga Nov 27 '12 at 9:57
Well just experiment with the query. I've picked something that really returns rows for me (but it was VF & controller error, not from batch so maybe in batches you get different error message). Change the WHERE clause to LogUserId = 'your id' AND StartTime >= TODAY (or whatever is appropriate)? Or initially remove WHERE completely, check the results, sort by Status... I strongly believe it'll work ;) – eyescream Nov 27 '12 at 10:03

Log all the error messages in a custom object .This is the best method i have heard so far.Keep the batch Id and record Id that failed and the error message in the custom object for each batch run.You can clean this for every month to avoid huge data in this .

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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