I have a class for an app manifest that is downloaded using Retrofit and stored locally:
class ArtifactArray { @Expose @SerializedName("artifacts") var artifacts: ArrayList<Artifact>? = null } class ManifestFile { var lastUpdate: Long? = null @Expose @SerializedName("device_id") var deviceId: String = "" @Expose @SerializedName("device_ip") var deviceIp: String = "" @Expose @SerializedName("artifacts") var artifactsToBeDownloaded: ArrayList<Artifact>? = null @Expose @SerializedName("active") var activeArtifacts: ArtifactArray? = null @Expose @SerializedName("downloaded") var downloadedArtifacts: ArtifactArray? = null } Note that when the object is fetched from the backend, the "activeArtifacts" and "downloadedArtifacts" properties are not present. Those fields are only used for local instances of this object. Not sure if that is relevant or not.
After the object is downloaded, I attempt to take the objects from the artifactsToBeDownloaded ArrayList and put them in the downloadedArtifacts ArrayList:
manifestFileToBeStored.downloadedArtifacts?.artifacts = arrayListOf<Artifact>() AppLog.i(TAG,"saveManifestFileToLocal() - Is downloaded artifacts array null [${manifestFileToBeStored.downloadedArtifacts?.artifacts == null}]") for (artifact in manifestFileToBeStored.artifactsToBeDownloaded!!){ AppLog.i( TAG,"saveManifestFileToLocal() - Type of artifact to be downloaded[${artifact.type}]") manifestFileToBeStored.downloadedArtifacts?.artifacts?.add(artifact) } Despite being initialized immediately before, the list of downloaded artifacts returns null in that first log statement. The list the items are being added from is full, and values are being returned in that second log statement. How can an ArrayList be null immediately after it is initialized?