Throwing null ptr exception in next()