Search code examples
androidfirebasekotlinnumberformatexceptionfirebase-remote-config

error java.lang.NumberFormatException: For input string: "008000 " in remote config in firebase


i have been implementing remote config using firebase referring this --> https://www.warmodroid.xyz/tutorial/firebase/how-to-set-firebase-remote-config-values-in-android/

i took one textview in layout to test but it crashing with java.lang.NumberFormatException: For input string: "008000 "

here is my code:---

   {
    val configSettings = FirebaseRemoteConfigSettings.Builder()
       // .setDeveloperModeEnabled(BuildConfig.DEBUG)
        .setMinimumFetchIntervalInSeconds(3600L)
        .build()
    mFirebaseRemoteConfig?.setConfigSettingsAsync(configSettings)

    getRemoteConfigValues()
    }
   .
   .
    .
    .
 private fun getRemoteConfigValues() {
    //here I have set the cache expiration duration to 1 hour
    //It means app will refresh after every 1 hour to check
    // if some changes are there in remote config
    var cacheExpiration: Long = 3600

    mFirebaseRemoteConfig.fetch(cacheExpiration).addOnCompleteListener(this) { task ->
        if (task.isSuccessful) {
            Toast.makeText(applicationContext, "Fetch Succeeded", Toast.LENGTH_SHORT).show()
            mFirebaseRemoteConfig.fetchAndActivate()
        } else {
            Toast.makeText(applicationContext, "Fetch Failed", Toast.LENGTH_SHORT).show()
        }
        //changing the textview and backgorund color
        setRemoteConfigValues()
    }
}

private fun setRemoteConfigValues() {
    val remoteValueBackground = mFirebaseRemoteConfig.getString(COLOR_CONFIG_KEY)
    if (remoteValueBackground.isNotEmpty()) {
        huuuuuu?.setBackgroundColor(Color.parseColor(remoteValueBackground))
    }
}

i debug this line huuuuuu?.setBackgroundColor(Color.parseColor(remoteValueBackground)) for value it shows remoteValueBackground="#0080000\t" in debugger though im passing a proper value from remote config firebase console

test case:--

  1. if i pass hardcoded value in huuuuuu?.setBackgroundColor(Color.parseColor(#008000)) --> it worked

  2. if i pass remoteValueBackground.trim() method it worked

but why this huuuuuu?.setBackgroundColor(Color.parseColor(remoteValueBackground)) is giving "#008000\t"

enter image description here

thanks need help


Solution

  • Your error message says there's a space in the string, see?

    java.lang.NumberFormatException: For input string: "008000 "
                                                              ^
    

    so it can't be parsed as an integer.

    edit it's actually a tab character according to this:

    "#008000\t"
    

    that's why .trim() works, it removes the whitespace either way. Is that character present in the Firebase text field? Maybe try typing it in again (especially if you pasted it in)