0
votes

I am trying to set the property of an object I've created in Android Studio using Kotlin. I am using a for loop to make a new object each time and add it to an array. When I initialize my object and try to set the topId it says "Val cannot reassigned" even though I'm declaring it a var.

 for (i in 1..5) {


                var topRanNum = generateRandomNum(topSize)
                var top = currentSeasonTops[topRanNum]
                var topLoopCounter = 0
                var topId = top.id
                var newOutfit: Outfit = Outfit()

                if(top.wornCount < 5 ) {

                    newOutfit.topId = top.id


                }

            }

Outfit Class

 public Outfit() {}

    public Outfit(Long topId, Long bottomId, String topPhotoPath, String bottomPhotoPath) {
        this.topId = topId;
        this.bottomId = bottomId;
        this.topPhotoPath = topPhotoPath;
        this.bottomPhotoPath = bottomPhotoPath;

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTopPhotoPath() {
        return topPhotoPath;
    }

    public String getBottomPhotoPath() {
        return bottomPhotoPath;
    }

 public Long getTopId() {
        return topId;
    }

    public Long getBottomId() {
        return bottomId;
    }


1
Please post here Outfit - Giorgio Antonioli
Hi Giorgio, I added the Outfit class - olivia.dayaram
What is the error you are getting? - OhhhThatVarun
When I initialize my object and try to set the topId it says "Val cannot reassigned" even though I'm declaring it a var. - olivia.dayaram

1 Answers

0
votes

This happens because you need to declare an accessible setter from outside in Java for topId or make the variable accessible from outside.

E.g.

public void setTopId(Long topId) {
    this.topId = topId;
}