如何修复Kotlin.KotlinNullPointerException错误?

时间:2020-06-02 12:50:33

标签: java android android-studio kotlin android-developer-api

我是Android开发的新手。这是我要构建的新闻应用程序。我正在测试我的应用程序,但未运行。当我打开一个应用程序时,它每次都崩溃,并且出现致命错误。 这是我的日志

FATAL EXCEPTION: main
    Process: com.example.newswrld, PID: 11271
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newswrld/com.example.newswrld.MainActivity}: kotlin.KotlinNullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3341)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3485)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2045)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7478)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)
     Caused by: kotlin.KotlinNullPointerException
        at com.example.newswrld.Remote.RetrofitClient.getClient(RetrofitClient.kt:16)
        at com.example.newswrld.Common.Common.getNewsService(Common.kt:11)
        at com.example.newswrld.MainActivity.onCreate(MainActivity.kt:35)
        at android.app.Activity.performCreate(Activity.java:7989)
        at android.app.Activity.performCreate(Activity.java:7978)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3316)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3485) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2045) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7478) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)

这是RetrofitClient.kt

package com.example.newswrld.Remote

import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

object RetrofitClient {
    var retrofit:Retrofit?=null

    fun getClient(baseUrl:String?):Retrofit{
        if(retrofit==null){
            retrofit==Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
        }
        return retrofit!!
    }
}

0 个答案:

没有答案