SafeEncoder Bug: Added null check before encoding

I added a NULL Check here since there is a scenario where a Lua script can return a NULL response that causes an NPE here.