A full-blown union type is indeed one way to solve it, but adding union types to Java would not solve it, since Java’s
String type already allows
null. First step is to fix this mistake in type-system. So, more type-safe languages start with not allowing
String type and then add some mechanism to deal with the case of “value missing” (union type is just one way to deal with it).