I do know how to handle the error; however, that does not solve the problem.
Your code interrupts the normal Application Life Cycle and it should make sure the normal life cycle is at least initialized. Also, you should document the fact that you are doing so.