Add more code to debug the problem then. Print what is happening at each stage - you will sooner or later find where it's going wrong.
Note: You should aim to change the code as little as possible when adding these prints. Just going round making random changes to the code isn't really that helpfull unless you have a fair idea what you're trying to fix.
Is your code compiling without warnings now?