Hello everybody, What is the reason of this error?...
# android
h
Hello everybody, What is the reason of this error?  And how can I fix it?   " FATAL EXCEPTION: OkHttp Dispatcher     Process: net.simplifiedcoding, PID: 13491     kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized" in Fragment
Copy code
fun reqBody(): RequestBody {
    val clientIdentifier = binding.edittxtClientIdentifier.text.toString().trim()
    val device = binding.edittxtDeviceType.text.toString().trim()

    val lang = binding.edittxtLang.text.toString().trim()
    val partn = binding.edittxtPartnerId.text.toString().trim()
    val pass = binding.editTextTextPassword.text.toString().trim()
    viewModel.login(
        clientIdentifier,
        Integer.parseInt(device),
        lang,
        Integer.parseInt(partn),
        pass
    )



    val rootObject = JSONObject()
    rootObject.put("ClientIdentifier", clientIdentifier)
    rootObject.put("DeviceType", device)
    rootObject.put("LanguageId", lang)
    rootObject.put("PartnerId", partn)
    rootObject.put("Password", pass)
    encrypt(rootObject.toString(), publicKey)

    val encrypted = encrypt(rootObject.toString(), publicKey)
    return RequestBody.create(
        "application/json; charset=utf-8".toMediaTypeOrNull(),
        "{\r\n    \"Data\":\"$encrypted\"\r\n}"
    )
}
In another class
Copy code
private fun <Request> getRetrofitClient(authenticator: Authenticator? = null): OkHttpClient {

    return OkHttpClient.Builder()
        .addInterceptor { chain ->
            chain.proceed(chain.request().newBuilder().also {
                it.addHeader("Accept", "application/json")
                it.method("POST", LoginFragment().reqBody())      //Error shows on this

            }.build())
        }.also { client ->
            authenticator?.let { client.authenticator(it) }
            if (BuildConfig.DEBUG) {
                val logging = HttpLoggingInterceptor()
                logging.setLevel(HttpLoggingInterceptor.Level.BODY)
                client.addInterceptor(logging)
            }
        }.build()

}
v
Actually this variable "binding" is not initialised and this variable declare as lateinit, if you initialise this issue will resolved.
h
@Vibhanshu Tiwari thanks for your reply. How can binding initialized in other functions?
Copy code
@AndroidEntryPoint
class LoginFragment : Fragment(R.layout.fragment_login) {

    private lateinit var binding: FragmentLoginBinding
    private  val viewModel:AuthViewModel by viewModels()

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        binding = FragmentLoginBinding.bind(view)
......
v
Your welcome, regarding your second query always initialise "binding" variable on "onCreate" method for an Activity and "onCreateView" for Fragment.
h
@Vibhanshu Tiwari, binding in getRetrofitClient() doesn't work, because binding is not initialized. How can I fix this issue?
Copy code
fun <Request> getRetrofitClient(authenticator: Authenticator? = null): OkHttpClient {
      
        val clientId = binding.edittxtClientIdentifier.text.toString().trim()     //Error shows on this
   
        Log.d("getRetrofitClient", "clientId:\t $clientId")
        return OkHttpClient.Builder()
            .addInterceptor { chain ->
                chain.proceed(chain.request().newBuilder().also {
                    it.addHeader("Accept", "application/json")
                  

                }.build())
            }.also { client ->
                authenticator?.let { client.authenticator(it) }
                if (BuildConfig.DEBUG) {
                    val logging = HttpLoggingInterceptor()
                    logging.setLevel(HttpLoggingInterceptor.Level.BODY)
                    client.addInterceptor(logging)
                }
            }.build()
}
v
ok got it, actually what happened is in your retrofit call before view binding initialised can you check or share when you call getRetrofitClient this method.
h
@Vibhanshu Tiwari if I don't use the 2nd line, the app works. I want to know a way that can initialize binding in other functions.
v
ok, i don't think so there is an other way for initialisation of that "binding" variable, because "binding" variable is related to the ui xml and ui xml in android initialisation on onCreate or onCreateView life-cycle methods.
h
@Vibhanshu Tiwari And what solution do you offer? 🙂
v
ok, i think you need to declare a separate method or update reqBody() method with one parameter that pass the value of binding.edittxtClientIdentifier.text.toString().trim() and call this method after onCreate or onCreateView because after onCreate method "binding" view will get initialised.