At least one of the planets (earth, mars, saturn, jupiter, uranus, neptune, pluto) is of nullable type Planet? hence the inferred type of arrayListOf(earth, ...) is ArrayList<Planet?>.
Since ArrayList<Planet> is not contravariant on type Planet it cannot be safely to assigned with value ArrayList<Planet?>.
To resolve the problem you can:
- make sure all planets are of not nullable type
Planet
if the above is not feasible change
var planets: ArrayList<Planet>? = null
to
var planets = arrayListOf<Planet?>()
filter out null planets and then assign the result collections to planets:
planets = arrayListOf(*arrayListOf(earth, ...).filterNotNull().toTypedArray())
Another way to make the compiler happy is to make the planets contravariant like so:
var planets: ArrayList<in Planet>? = null
PS. Use kotlin collection types List<T>, Set<T> and corresponding listOf, setOf instead of Java's counterparts whenever possible.