chain.request() in interceptor in Retrofit2 returns null
<p>I am trying to add HMAC to the header. It is working fine. But randomly on some devices, it throws NPE. Below is the complete code.</p>

<p>Using okhttp v3.4.1, okio v1.9.0 &amp; retrofit v2.1.0</p>

<pre><code>OkHttpClient.Builder okHttp = new OkHttpClient().newBuilder();
okHttp.connectTimeout(Constants.CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
okHttp.readTimeout(Constants.CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
okHttp.writeTimeout(Constants.CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
okHttp.retryOnConnectionFailure(false); 
okHttp.addInterceptor(new Interceptor() {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request.Builder requestBuilder;
        Request request = null;

        try {
            String requestUrl = chain.request().url().toString();       //NPE issue : Attempt to invoke virtual method 'okhttp3.HttpUrl okhttp3.Request.url()' on a null object reference

            requestBuilder = chain.request().newBuilder()
                    .addHeader("hmac", generateHmac(requestUrl))
                    .addHeader("somemoreHeaders", "values");

            if(additionalHeaders != null &amp;&amp; !additionalHeaders.isEmpty()) {
                Set&lt;Map.Entry&lt;String, String&gt;&gt; entries = additionalHeaders.entrySet();

                for (Map.Entry&lt;String, String&gt; header : entries) {
                    requestBuilder.addHeader(header.getKey(), URLEncoder.encode((auth.getEncodedData(header.getValue())), context.getString(R.string.charSet)));
                }
            }

            request = requestBuilder.build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return chain.proceed(request);
    }
});

okHttp.build();
</code></pre>

<p>Here is my exception:</p>

<pre><code>Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'okhttp3.HttpUrl okhttp3.Request.url()' on a null object reference
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(SourceFile:111)
    at okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:92)
    at okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:67)
    at mypackage.client.RetrofitClient$2.intercept(SourceFile:112)
    at okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:92)
    at okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:67)
    at okhttp3.RealCall.getResponseWithInterceptorChain(SourceFile:170)
    at okhttp3.RealCall.access$100(SourceFile:33)
    at okhttp3.RealCall$AsyncCall.execute(SourceFile:120)
    at okhttp3.internal.NamedRunnable.run(SourceFile:32)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
    at java.lang.Thread.run(Thread.java:818)
</code></pre>