you must ensure the pointers are != null. just do that in your while-loop.