Fix NullPointerException when selecting elements