Why does the following work fine?
String str; while (condition) { str = calculateStr(); ..... }
But this one is said to be dangerou
You have a risk of NullPointerException if your calculateStr() method returns null and then you try to call a method on str.
NullPointerException
calculateStr()
More generally, avoid having variables with a null value. It stronger for class attributes, by the way.