class TransportService {
companion object {
private val waitressCallRestClient = RestClientFactory.createRestClient(WaitressCallRestClient::class.java)
private val TAG = TransportService::class.java.name
init {
Debug.d(TAG, "TransportService_initialize")
}
}
}
здесь RestClientFactory.kt
object RestClientFactory {
private val gsonBuilder = GsonUtil.gsonbuilder
var gson: Gson? = null
private set
private val CONNECTION_TIME_OUT_SEC = 60
private val READ_TIME_OUT_SEC = 60
private val WRITE_TIME_OUT_SEC = 60
private var httpClient: OkHttpClient.Builder? = null
private val httpLoggingInterceptor = HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY)
private val TAG = RestClientFactory::class.java.name
private val builder = Retrofit.Builder()
.baseUrl(BuildConfig.API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson!!))
.addConverterFactory(QueryConverterFactory.create())
.client(httpClient!!.build())
var retrofit = builder.build()
private set
fun <T> createRestClient(restClientClass: Class<T>): T {
retrofit = builder.build()
return retrofit.create(restClientClass)
}
}
Но я получаю ошибку времени выполнения в этой строке:
.addConverterFactory(GsonConverterFactory.create(gson!!))
Ошибка:
11-08 13:26:58.104 E/AndroidRuntime(13653): FATAL EXCEPTION: main
11-08 13:26:58.104 E/AndroidRuntime(13653): Process: com.myproject.android.debug, PID: 13653
11-08 13:26:58.104 E/AndroidRuntime(13653): java.lang.ExceptionInInitializerError
11-08 13:26:58.104 E/AndroidRuntime(13653): at com.myproject.android.service.TransportService.<clinit>(TransportService.kt:25)
11-08 13:26:58.104 E/AndroidRuntime(13653): at com.myproject.android.viewmodel.FeedbackViewModel$doClickSend$1.invokeSuspend(FeedbackViewModel.kt:39)
11-08 13:26:58.104 E/AndroidRuntime(13653): at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
11-08 13:26:58.104 E/AndroidRuntime(13653): at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:241)
11-08 13:26:58.104 E/AndroidRuntime(13653): at android.os.Handler.handleCallback(Handler.java:739)
11-08 13:26:58.104 E/AndroidRuntime(13653): at android.os.Handler.dispatchMessage(Handler.java:95)
11-08 13:26:58.104 E/AndroidRuntime(13653): at android.os.Looper.loop(Looper.java:148)
11-08 13:26:58.104 E/AndroidRuntime(13653): at android.app.ActivityThread.main(ActivityThread.java:5417)
11-08 13:26:58.104 E/AndroidRuntime(13653): at java.lang.reflect.Method.invoke(Native Method)
11-08 13:26:58.104 E/AndroidRuntime(13653): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-08 13:26:58.104 E/AndroidRuntime(13653): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-08 13:26:58.104 E/AndroidRuntime(13653): Caused by: kotlin.KotlinNullPointerException
11-08 13:26:58.104 E/AndroidRuntime(13653): at com.myproject.android.api.RestClientFactory.<clinit>(RestClientFactory.kt:40)
11-08 13:26:58.104 E/AndroidRuntime(13653): ... 11 more