Fixing null pointer exception and eternal loop in isSelected