Kanan
Kanan

Reputation: 177

retrofit Give kotlin.KotlinNullPointerException

Data get from the Sql server and get data json. this json data parsing retofit2. Created Login Activity but its give error

MainActivity.kt

class MainActivity : AppCompatActivity() {


    internal lateinit var api : APIInterface

    private var compositeDisposable : CompositeDisposable? = null

    var userName : String? = null

    var password : String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btnLogin.setOnClickListener {

            userName = tvUsername.text.toString()
            password = tvPassword.text.toString()

            getUserName(userName!!,password!!)
        }
    }

    fun getUserName(user : String, pass : String){


        val retrofit = APIClient.apIClient
        if (retrofit != null) {
            api = retrofit.create(APIInterface::class.java)
        }
        compositeDisposable!!.add(api.getLoginData(user, pass)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    if (it.success.equals(1)){

                    val intent = Intent(this,Company::class.java)
                    startActivity(intent)

                    Toast.makeText(this,"Login Successfully!!!",Toast.LENGTH_LONG).show()
                }else{

                    Toast.makeText(this,"UserName or Password is Wrong!!!",Toast.LENGTH_LONG).show()
                }
                },{
                    Toast.makeText(this, it.message, Toast.LENGTH_LONG).show()
                })
        )
    }
}

when Debbuger reached on compositeDisposable!!.add(api.getLoginData(user, pass) it's give Error kotlin.kotlinNullPointerException

RestApi Url :

http://localhost/Account/Login.php?user=ABC&pass=1

APIClient.kt

object APIClient {
   val BASE_URL = "http://10.0.2.2/Account/"
    var retrofit:Retrofit? = null
    val apIClient:Retrofit?
        get() {
            if (retrofit == null)
            {
                retrofit =  Retrofit.Builder().
                        baseUrl(BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                        .build()
            }
            return retrofit
        }


}

APIInterface.kt

interface APIInterface {
 @GET("Login.php")
    fun getLoginData(@Query("user") user : String,
                     @Query("pass") pass : String) : Observable<LoginList>
}

Upvotes: 2

Views: 728

Answers (1)

user2340612
user2340612

Reputation: 10703

The most likely cause for the NullPointerException is that compositeDisposable is null.

At the beginning of MyActivity that variable is initialised to null and then it's never changed, so when you use the !! operator the exception is thrown. I think you can initialise compositeDisposable directly with the correct value, i.e. something like val compositeDisposable = CompositeDisposable().

Also, val should be preferred over var whenever possible – as immutability is easier to control – and userName and password could probably be local variable or at least private

Upvotes: 1

Related Questions