0

When trying to 'add' a custom object to an ArrayList, the ArrayList remains null

I've tried modifying the data class initialization by using MutableLists, among other things

Here is the data class that I'm using:

data class WYRStatistics(val team: String, val league: String, val gp: String, val g: String, val a: String, val pt: String, val pim: String, val pm: String)

This is where I'm using the data class to construct on object and try to add the object to an ArrayList

var n = 0
        var statsArr: ArrayList<WYRStatistics>? = null

        while (n < tempStats.length()) {

            val statObject = tempStats.getJSONObject(n)

            val singleStat = WYRStatistics(statObject.getString("Team"),
                                statObject.getString("League"),
                                statObject.getString("GamesPlayed"),
                                statObject.getString("Goals"),
                                statObject.getString("Assists"),
                                statObject.getString("Points"),
                                statObject.getString("PenaltyMinutes"),
                                statObject.getString("PlusMinus")
                            )
            println(singleStat)
            statsArr?.add(singleStat)
            println(statsArr)
            tempPlayer.stats?.add(singleStat)
            println(tempPlayer)
            n++
        }

The utilization is inside of a function that handles the asynchronous task of decoding JSON. This is all working fine.

tempStats is a JSONArray - when printed it is formatted properly

When I println singleStat, the object is printed properly

However, when I 'add' singleStat to statsArr, the statsArr returns null

3
  • Well, yes. You're explicitly initializing to null, and never assign any no null value to the variable. list?.add() means: if list if not null, then add, otherwise do nothing. Avoid nulls as much as you can. If you need a list, create a list. And make it a val, too, since it's not supposed to be reassigned, is it? Commented Jul 13, 2019 at 16:04
  • Does that mean you can never 'add' anything to a list that is null? Commented Jul 13, 2019 at 16:27
  • That sentence doesn't even make sense. A list can't be null. A variable can be null. If a variable's value is null, then it means that it doesn't refer to any object, so there is no list at all. You can't add anything to a list that doesn't exist. null doesn't mean "an empty list". null means "no list at all". Commented Jul 13, 2019 at 16:30

1 Answer 1

1

You are initializing arrayList to nullas what JB mentioned.

The correct way should be

val statsArr =  arrayListOf<WYRStatistics>()
Sign up to request clarification or add additional context in comments.

2 Comments

Why make it nullable? Why use a var?
: should be = .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.