That is a huge amount of code to wade through. Break the problem down, make sure your variables have been initialized, and try created an SSCCE that demonstrates the problem without all the excess...