kodecocodes/adva-materials

Variable 'interceptedRequest' must be initialized

yazmnh87 opened this issue · 1 comments

I'm working through real-world-android-by-tutorials Chapter 4.4 Interceptors, Im following the code as far as I can see and am getting an error "Variable 'interceptedRequest' must be initialized " adding in the code block from "Handling valid tokens"

this is the function

override fun intercept(chain: Interceptor.Chain): Response {
  // Replace with your code
    val token = preferences.getToken()
    val tokenExpirationTime =
        Instant.ofEpochSecond(preferences.getTokenExpirationTime())
    val request = chain.request()
//      if(chain.request().headers[NO_AUTH_HEADER] != null) return
//      chain.proceed()
    val interceptedRequest: Request

    if(tokenExpirationTime.isAfter(Instant.now())){
        interceptedRequest =
            chain.createAuthenticatedRequest(token)
    }else{

    }
    return chain.proceedDeletingTokenIfUnauthorized(interceptedRequest)
  //return chain.proceed(chain.request())
}

on the 17th line the method being called "chain.proceedDeletingTokenIfUnauthorized" and being passed "interceptedRequest", its throwing that error 'Variable 'interceptedRequest' must be initialized' but not sure why, the code is exactly what it appears as in the instruction. Maybe Im just getting poor vision, but I've tried invoking the "val" interceptedRequest(), but it doesnt like that, "initialized" seems like it is expecting to be assigned...

any help is greatly appreciated, I'll keep looking at it, I hope I do not need glasses. TIA

Hi there! First of all, so sorry for the ridiculously late reply. I thought I had notifications on this, but apparently I didn't 🤦

There's a bit right at the beginning of the "Handling valid Tokens" section that tells you to add a block of code to the intercept method, and one of the lines initializes interceptedRequest. Maybe you missed it? 🤔