Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I have an OkHttp client which uses my custom network interceptor in order to insert token into requests header.
I keep on getting the exception
E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.edstart.tazuzu, PID: 32093
java.lang.NullPointerException: interceptor com.edstart.tazuzu.RestAPI.RequestsErrorInterceptor@2bde70f returned null
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:157)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at com.edstart.tazuzu.RestAPI.TazuzuRequestsInterceptor.intercept(TazuzuRequestsInterceptor.kt:30)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:254)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:200)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
I have tried surrounding the interceptor with try/catch. and also throwing error in order to avoid app crash but with no good results.
The interceptor code:
package com.edstart.tazuzu.RestAPI
import android.util.Log
import com.edstart.tazuzu.Managers.ServerManager
import okhttp3.Interceptor
import okhttp3.Response
import java.net.InetAddress
import kotlin.Exception
* interceptor for the tazuzu api requests to insert the token before the request
* send to the server (for authentication)
class TazuzuRequestsInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain?): Response? {
try {
if (!isInternetAvailable()) {
throw Exception()
} else {
var request = chain?.request()
if (request != null) {
val builder = request.newBuilder()
if (builder != null) {
builder.addHeader("x-access-token", ServerManager.token)
request = builder.build()
if (request != null && chain != null) {
return chain.proceed(request)
} catch (e: java.lang.Exception) {
Log.e("###", e.toString())
throw e
return null
private fun isInternetAvailable(): Boolean {
return try {
val ipAddr = InetAddress.getByName("google.com")
//You can replace it with your name
!ipAddr.equals("")
} catch (e: Exception) {
false
Please help, looking for ways controlling exceptions in a way it won't make the app crash.
–
class ErrorHandlingInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val response = chain.proceed(request)
if (!isInternetAvailable()) {
// Do your thing
// 1. Either throw a generic exception
// 2. Throw an exception with your own custom status code that can be checked on the callbacks
// 3. Create an interface callback
// example throw invocation
throw Exception()
} else {
val builder = request.newBuilder()
builder?.let { builder ->
builder.addHeader("x-access-token", ServerManager.token)
request = builder.build()
// No need for this additional checks
// if (request != null && chain != null) { ... }
return chain.proceed(request)
private fun isInternetAvailable(): Boolean {
return try {
val ipAddr = InetAddress.getByName("google.com")
//You can replace it with your name
!ipAddr.equals("")
} catch (e: Exception) {
false
If for example, you have chosen to throw an exception when there's no internet connection, you could just handle everything in onFailure()
callback of the retrofit
call.
Or if you are using rxjava
, you can just subscribe for the error on the error callback handler.
Read more on
https://futurestud.io/tutorials/retrofit-2-catch-server-errors-globally-with-response-interceptor
https://medium.com/@tsaha.cse/advanced-retrofit2-part-1-network-error-handling-response-caching-77483cf68620
I had this error when tried to launch request in Main Thread. Try to use coroutines, just wrap your Retrofit-request call with CoroutineScope:
val apiService = RetrofitFactory.makeRetrofitService()
CoroutineScope(Dispatchers.IO).launch {
val response = apiService.myPostRequest()
// process response...
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.