Fix '... of null' exception when removing lines at the end of a file