I got it fixed.

The error was coming due to calling of the events in the wrong order.