It’s debugging time, I guess. Debugging is always done via the same procedure:
- Identify the exact place in the code where the problem manifests itself
- Examine the program’s state at that moment to find out which of the involved values is wrong
- Trace the wrong value back to its origin
You can use debug printing or request debug mode for your project.