初始化改造时出现错误:KotlinNullPointerException

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
susewxy 回答:初始化改造时出现错误:KotlinNullPointerException

httpClientgson为空,您永远不会初始化它们。

使用!!停止,编译器会告诉您;)

,
   1) try using this,If you do not want to use gson in this class

        private var retrofit: Retrofit? = null
         retrofit = Retrofit.Builder()
                        .baseUrl(BASE_URL)
                        .client(httpClient.build())
                        .addConverterFactory(GsonConverterFactory.create())
                        .build()

    2) or if you want to use gson here then below code may help you :
    Gson gson = new GsonBuilder()
         .registerTypeAdapter(Id.class,new IdTypeAdapter())
         .enableComplexMapKeySerialization()
         .serializeNulls()
         .setDateFormat(DateFormat.LONG)
         .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
         .setPrettyPrinting()
         .setVersion(1.0)
         .create();
        Retrofit retrofit = new Retrofit.Builder()  
         .baseUrl(BASE_URL)
         .addConverterFactory(GsonConverterFactory.create(gson))
         .build();
,

您需要初始化Gson对象。

Gson gson = new GsonBuilder()
                .setLenient()
                .create();
,

我改成这个,现在可以工作了:

object RestClientFactory {
    private val gsonBuilder = GsonUtil.gsonbuilder
    private var gson: Gson
    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
    private val httpLoggingInterceptor =
        HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)

    private val TAG = RestClientFactory::class.java.name

    init {
        Debug.d(TAG,"static_initializer: START_GSON_setting!")
        gson = gsonBuilder.create();
        gsonBuilder.setDateFormat(DateUtil.TO_FO_DATETIME_FORMAT)
        // default timeout = 10 sec
        httpClient = OkHttpClient.Builder() // default timeout = 10 sec
            .connectTimeout(CONNECTION_TIME_OUT_SEC.toLong(),TimeUnit.SECONDS)
            .readTimeout(READ_TIME_OUT_SEC.toLong(),TimeUnit.SECONDS)
            .writeTimeout(WRITE_TIME_OUT_SEC.toLong(),TimeUnit.SECONDS)
,

替换

.addConverterFactory(GsonConverterFactory.create(gson!!))

.addConverterFactory(GsonConverterFactory.create(GsonBuilder().setLenient().create()))
本文链接:https://www.f2er.com/3138270.html

大家都在问