A NumberFormatException occurs in Java code when a programmer tries to convert a String into a number and the conversion fails.