Two solutions:
a) Use poor man's debugger (print to the console) or use a logging framework. After the error happens, analyze the output.
b) Write a test case that tries to reproduce the problem. Even if you can't find it that way, this will clean your code and sometimes solve the issue.