Java.lang.IndexoutofBoundSexception: Index: 0, Größe: 0 Fehler in Kotlin

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Java.lang.IndexoutofBoundSexception: Index: 0, Größe: 0 Fehler in Kotlin

by Anonymous » 01 Feb 2025, 09:04

Ich versuche, eine Währungserzieher -App mit einer API zu erstellen. Ich weiß, dass das Ausschüttungsanordnungsarray leer ist und ich kann nicht zu einem leeren Array greifen. Aber ich weiß nicht, wie ich es beheben soll.Column {
if(calculateViewModel.loading){
CircularProgressIndicator()
}else{
Text(text = calculateViewModel.exchangeDetail[0].get("EUR").toString())
Text(text = calculateViewModel.exchangeDetail[0].get(currency2).toString())
}

}
< /code>
Und dies wird CalculateViewModel < /p>
class CalculateViewModel: ViewModel() {

private val exchangeService = ExchangeService()
var loading: Boolean by mutableStateOf(false)
var errorMessage : String by mutableStateOf("")
var exchangeDetail : MutableList by mutableStateOf(mutableListOf())

fun getExchangeRatio(input1: String, input2: String) = viewModelScope.launch {
errorMessage = ""
loading = true
try {
val ratio = exchangeService.getExchangeCourse(input1,input2)
exchangeDetail.add(ratio.data)
}catch (e: Exception){
errorMessage = e.message.toString()
}finally {
loading = false
}
}

}
< /code>
Class < /p>
erregt < /p>
@kotlinx.serialization.Serializable
data class ExchangeDto (

val data: Map

//Zugriff über .get()
)
< /code>
Stadt < /p>

class APIManager {
private val apiKey = "Om1xoJkPAxk9neFbFvzezQtxAuNHdSERTfN8CPbk"
var jsonHttpClient = HttpClient {
expectSuccess = true
install(ContentNegotiation) {
json(Json {
ignoreUnknownKeys = true
})
}
defaultRequest {
url.host = "api.freecurrencyapi.com"
url.protocol = URLProtocol.HTTPS
url.encodedPath = "/v1/latest?apikey=$apiKey" + url.encodedPath
contentType(ContentType.Application.Json)
headers {
append(HttpHeaders.Authorization,
"Bearer $apiKey")
}

}

HttpResponseValidator {
getCustomResponseValidator(this)
}

}
}
< /code>
Thanks...
Ich habe versucht, den Währungswert aus einer Array -Liste zu erhalten. Ich hatte damit gerechnet, sie über Get () -Funktion aufzurufen. Ich habe einen IndexoutOfbounds -Fehler erhalten.

Top