Fix null pointer deference bug