Add 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 a NPE here.