when debug, set a breakpoint at the first line within the if, and check if your program can reach there or not. If not, we can think about other solutions
Maybe within your "if" statement, there are some unnoticeable clauses that are executed and case the problem. Just a guess. The best way is to paste your code, at least the fragment related to the "if" statement.