Hello, everybody, I've a question. I get an error ...
# android
Hello, everybody, I've a question. I get an error " kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized".  binding cannot initialized in getRetrofitClient().  What mistake did I make? Or there any way or other technologies to fix this issue?  If I don't use the 2nd line of getRetrofitClient(), the app works. Thanks in advance.
Copy code
lass LoginFragment : Fragment() {

    private lateinit var binding: FragmentLoginBinding
    private val viewModel: AuthViewModel by viewModels()
    /* private val viewModel:AuthViewModel by lazy{

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentLoginBinding.inflate(inflater, container, false)
      return binding.root

  fun reqBody(): String {

        Log.d("LogFrag", "reqBody's binding$binding")

        val clientIdentifier = binding.edittxtClientIdentifier.text.toString().trim()

        val pass = binding.editTextTextPassword.text.toString().trim()


        val rootObject = JSONObject()
        rootObject.put("ClientIdentifier", clientIdentifier)
        rootObject.put("Password", pass)
        encrypt(rootObject.toString(), publicKey)

        val encrypted = encrypt(rootObject.toString(), publicKey)

        return encrypted

fun <Request> getRetrofitClient(authenticator: Authenticator? = null): OkHttpClient {

    val finalEncr=reqBody()                //Error shows on this

    val body = "{\r\n    \"Data\":\"$finalEncr\"\r\n}"
        .toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull())
Copy code
return OkHttpClient.Builder()
        .addInterceptor { chain ->
            chain.proceed(chain.request().newBuilder().also {
                it.addHeader("Accept", "application/json")
                it.method("POST", body)

        }.also { client ->
            authenticator?.let { client.authenticator(it) }
            if (BuildConfig.DEBUG) {
                val logging = HttpLoggingInterceptor()
Copy code
class RemoteDataSource {

    fun <Api> buildApi(
        api: Class<Api>,
        context: Context
    ): Api {

        val authenticator = TokenAuthenticator(context, buildTokenApi())
        Log.d("RemoteDataSource", "buildApi")

        return Retrofit.Builder()
Copy code
    fun buildTokenApi(): TokenRefreshApi {
        Log.d("RemoteDataSource", "buildTokenApi")

        return Retrofit.Builder()

First of all, when pasting a lot of code, it is appreciated when it is inside a code snippet so it does not pollute the channel. Now, that error means that you are trying to access a variable (binding) before it is initialized. Maybe you have a logic error or a race condition.
you are using the view binding somewhere before it is assigned in the onCreateView lifecycle method.
I would suggest reviewing this page and following their guidelines about viewbinding in fragments: https://developer.android.com/topic/libraries/view-binding
Why are you creating a fragment just to get a retrofit client instance ?
@Luke He ignores the rules. Each post the same. I decide to ignore him. Hope others will do the same. Maybe he’ll stop
@Hovhannes please use threads!