java null errror on first run only

I have the following method that is called as part of a report generation.

controller.loadAllRecords();

controller.sort('etime_id desc');

var i = 0;
controller.setSelectedIndex(1);
while ( i < 2400 )
	{
	if(tim_etime_to_tim_timesheet.getSize() >= 1)
		{
		tim_etime_to_tim_timesheet.setSelectedIndex(1);
		if(tim_etime_to_tim_timesheet.approved == 'YES') { timesheet_approved = 'YES';}
		else { timesheet_approved = 'NO';}	
		}
	else { timesheet_approved = 'NO';}	
	var current = controller.getSelectedIndex();
	controller.setSelectedIndex(current + 1);
	i++;
	}

When I kick off this method it gives me the following error: java.lang.NullPointerExceptoin. I have debug on, and it doesn’t give me a line of code that is erroring. I won’t get the error right away, but after a few hundred records it will pop up. Also, it happens on different records, and at different times in the search.

Also, if I turn around and run the method again, it will run all the way through with no errors.

I am baffled as to what I am doing wrong. Any suggestions?

I think I found an answer to my own question.
When I insert an application.sleep(101); line into the loop, it seems to work fine.
My assumption is that the method gets going so fast that the related record search can’t keep up and at some random point will error out.