Since you are checking whether or not the variable is null, once it becomes null and stops the loop it could be throwing that exception. I'd try surrounding the while loop in a try-catch block and go...