When you can't find the bug, use bisection. Comment out half the code. Does the error still happen?
If yes, the bug is in the remaining half. If no, the bug is in what you commented out. Repeat until you find the exact line.
This works for runtime errors, wrong outputs, and performance issues. It's methodical and always converges on the problem.