0

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?

1 Answer 1

2

You don't initialize the list on the ArtifactArray object because var downloadedArtifacts: ArtifactArray is still null. When you do:

manifestFileToBeStored.downloadedArtifacts?.artifacts = arrayListOf<Artifact>() 

you basically say: "If the property downloadedArtifacts of the object manifestFileToBeStored is not null (notice the ?), then it's property artifacts is a new arrayListOf.

What you probably want to do is:

manifestFileToBeStored.downloadedArtifacts = ArtifactArray() 

and then:

manifestFileToBeStored.downloadedArtifacts?.artifacts = arrayListOf<Artifact>() 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.