read the whole passage of genesis 38 you will see that he was told to have relations with his sister inlaw and because the offspring was not going to be his own he decided to not have any and directly disobay god by waisting his seed. thus his death