Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Bug Report: NullPointerException when trying to fetch teams list #30

Closed
2 tasks done
Narmo opened this issue Jun 14, 2023 · 5 comments
Closed
2 tasks done
Assignees
Labels
bug Something isn't working

Comments

@Narmo
Copy link

Narmo commented Jun 14, 2023

👟 Reproduction steps

When I try to fetch teams list using Teams object, I get NPE.

Demo code:

val client = Client().setEndpoint(Application.appWriteEndpoint).setProject(Application.appWriteProjectId).setKey(Application.appWriteApiKey)
val teamsClient = Teams(client)

val teams = teamsClient.list().teams // this line causes NPE, see below

teams.forEach {
    println(it.name)
}

👍 Expected behavior

Teams list should load.

👎 Actual Behavior

The call crashes with following stack trace:

Exception in thread "OkHttp Dispatcher" java.lang.NullPointerException: null cannot be cast to non-null type kotlin.collections.Map<kotlin.String, kotlin.Any>
	at io.appwrite.models.Team$Companion.from(Team.kt:83)
	at io.appwrite.models.TeamList$Companion.from(TeamList.kt:43)
	at io.appwrite.services.Teams$list$converter$1.invoke(Teams.kt:43)
	at io.appwrite.services.Teams$list$converter$1.invoke(Teams.kt:42)
	at io.appwrite.Client$awaitResponse$2$1.onResponse(Client.kt:507)
	at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:833)

I've investigated the crash and found the point. The error occurs in mapping the Team object's preferences in Team.kt file:

@Suppress("UNCHECKED_CAST")
fun <T> from(
    map: Map<String, Any>,
    nestedType: Class<T>
) = Team<T>(
    id = map["\$id"] as String,
    createdAt = map["\$createdAt"] as String,
    updatedAt = map["\$updatedAt"] as String,
    name = map["name"] as String,
    total = (map["total"] as Number).toLong(),
    prefs = Preferences.from(map = map["prefs"] as Map<String, Any>, nestedType),
)

In last line, where prefs is assigned, actual value of maps["prefs"] is null, while cast expects it to be Map<String, Any>. So the prefs field in data class Team should be nullable, and I suggest that the cast should be rewritten, something like that:

prefs = (map["prefs"] as? Map<String, Any>)?.run { prefs = Preferences.from(map = this, nestedType),

🎲 Appwrite version

Version 1.3.x

💻 Operating system

MacOS

🧱 Your Environment

I use io.appwrite:sdk-for-kotlin:2.0.0 which automatically fetches all required dependencies.

👀 Have you spent some time to check if this issue has been raised before?

  • I checked and didn't find similar issue

🏢 Have you read the Code of Conduct?

@abnegate
Copy link
Member

Hey @Narmo, in this case, prefs should not be null. It is supposed to default to an empty map. Did you happen to recently upgrade from a previous Appwrite version?

@Narmo
Copy link
Author

Narmo commented Jun 16, 2023

Hey @Narmo, in this case, prefs should not be null. It is supposed to default to an empty map. Did you happen to recently upgrade from a previous Appwrite version?

Hello @abnegate Yes, we've recently updated our server instance from I think 1.2.x.

@abnegate
Copy link
Member

Hello @abnegate Yes, we've recently updated our server instance from I think 1.2.x.

@Narmo Please check if the migration was run, that should be adding the default values for team prefs: https://appwrite.io/docs/upgrade#migration

@abnegate abnegate self-assigned this Jun 19, 2023
@Narmo
Copy link
Author

Narmo commented Jun 21, 2023

@Narmo Please check if the migration was run, that should be adding the default values for team prefs: https://appwrite.io/docs/upgrade#migration

@abnegate Sorry, my mistake. We did not update the server instance to latest (1.3.7) version. After the update and migration everything works fine with latest SDK version.

So I think that this ticket can be closed now.

@abnegate
Copy link
Member

@Narmo Awesome glad it's sorted 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants