1

I have a code:

fun loadSounds(): List<String> {
    try {
      val soundNames = assets.list(SOUNDS_FOLDER)!!
      Log.d (TAG, "Found ${soundNames.size} sounds")
      return soundNames.asList()
    }
    catch (e: Exception) {
      Log.e(TAG, "Could not list assets", e)
      return emptyList()
    }
  }

I couldn't compile it, because of error:

Unresolved reference: asList

How can I convert array (array<(out) string!>) to list? And what does array<(out) string!> mean?

Update: I have a promblems with asList(), toList() and split(), they are unresolved references.

6
  • After your edit, the error doesn't match the code anymore. Could you please update the error? This code works fine when I try locally with a mocked Java AssetsManager. Is this really the code you're using? Commented Jun 22, 2021 at 9:37
  • Unresolved reference: asList Commented Jun 22, 2021 at 9:52
  • Does this just appear in the IDE, or also when you build from command line? It might be because the Kotlin stdlib is not properly added, but I would be surprised since the Kotlin Gradle plugin includes it by default since Kotlin 1.4. Commented Jun 22, 2021 at 9:58
  • I don't know how to build from command line, so, yes, in the IDE (Android, not Intellij IDEA). I have in my build.gradle: dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" ... } Commented Jun 22, 2021 at 10:15
  • 1
    Definitely looks like you're missing stdlib functions. I tried to create a new project as well in Android Studio, and it works fine (I have the same line in Gradle defining the stdlib). Maybe File > invalidate caches/restart could help the IDE? You can also try to run ./gradlew build to test this on the command line. Commented Jun 22, 2021 at 10:28

4 Answers 4

3

what does array<(out) string!> mean?

This declaration has a bunch of components to unpack:

  • Array is the base raw type
  • String is the the type argument of Array, which in this case is the element type of the array
  • The exclamation mark on String! shows that it is a platform type. This means that this comes from Java and Kotlin doesn't know the nullability of these Strings. Basically, it can't tell you whether the arrays contains nulls or not. When in doubt like this, Kotlin doesn't force you to check the nullability, for convenience.
  • the out modifier on the generic type parameter shows that the array may contain subtypes of String, so you should not be able to update elements in it in theory (you should only be able to get values "out" of the array, and store them in String variables). Kotlin puts parentheses around the (out) because it's inferred from Java (nothing is certain). So in practice you might not be restricted like this, and you should still be able to assign the array to a variable of type Array<String>, and thus insert stuff in it.

In short, Array<(out) String!> means "Java array of Strings (or a subtype of String) which are nullable or not".


Now, I'm not very well versed in Android APIs, but it seems that the assets.list() function you're calling is this one.

This method returns a nullable array Array<out String!>?, so you can't call methods directly on it without checking for the nullability of the array itself. Either use a safe call with ? or check for null before returning:

return soundNames?.toList() // you have to make your return type nullable in this case

Or:

return soundNames?.toList() ?: error("Null assets array")

EDIT

After the edit in the question, it seems that the nullability is now already handled via !! operator, and that the problematic method is now asList() instead of toList(). So this is a different story.

Sign up to request clarification or add additional context in comments.

6 Comments

Checking the nullability doesn't do a trick, I have the same error that I mentioned. I have minSdkVersion 21, targetSdkVersion 30
@DmitryFedorov could you please add types to your declaration in your code? It would help answer the question. I don't know what the type of assets.list() is, so it's hard to tell how to help here. Please add val soundNames: TYPE_HERE = assets.list(), in a way that compiles. Also, what's the type of assets?
val soundNames: Array<(out) String!> and assets: private final val assets: AssetManager. And I was wrong, missing not soundNames.toList() function, but soundNames.asList() . And correct note was: val soundNames = assets.list(SOUNDS_FOLDER)!! . I.e. with "!!"
@DmitryFedorov The !! operatory simply throws an exception if list() returns null, just like the option I mentioned with the safe call ? + elvis ?: + error(). It's usually better to throw a more specific error like assets.list(..) ?: error("some specific text")
@DmitryFedorov you cannot write Array<(out) String!> in the code, this wouldn't compile. You probably have to write something like Array<String> instead, or Array<out String>. Also, why do you need asList()? Isn't toList() sufficient for you? asList() is only defined on Array<out String>, not on Array<String>, so depending on which type you choose for your variable, you'll have access to asList or not.
|
1

Try with the following code.

fun loadSounds(): List<String>? {
    try {
        val soundNames = assets.list ("")
        Log.d (TAG, "Found ${soundNames?.size} sounds")
        return soundNames?.toList()
    }
    catch (e: Exception) {
        Log.e(TAG, "Could not list assets", e)
        return emptyList()
    }
}

1 Comment

It doesn't work, I have the same error that I mentioned
0

The main reason was not in checking for the nullability, but in the project settings, something was broken (maybe stdlib including or something else). I copy my code and paste it in another empty project, rename it and everething is fine now.

Comments

0

I had the same problem but I solved it.

Check the "gradle : project" and Check if it's more than kotlin 1.5 version.

Comments

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.