Your error message indicates that there's a lot more to the problem than your code snippet is demonstrating. Please provide a complete, but minimal, testcase that I can cut'n'paste and run on my computer, otherwise I'm not sure any of us are going to be much help. Remember - I'm not sitting next to you, so you have to provide enough information for me to replicate your problem.
My criteria for good software:
- Does it work?
- Can someone else come in, make a change, and be reasonably certain no bugs were introduced?