Use your debugger. Set breakpoints all over the place (or be more intelligent, if you can), then step through until the line where the error occurs. The mistake should be somewhere before that point.