added null pointer check to gadget function