Subin T
Subin T

Reputation: 157

How to check if there is any data in content provider

Hi I am new to Android Development. Im working with Android Kotlin. I want to check whether there is any data in the content provider. This is the sample code that I tried in mainactivity

class MainActivity : AppCompatActivity() {
    companion object {
        val AUTHORITY = "com.wsample.moblity.auth"
        val CONTENT_URI = Uri.parse("content://$AUTHORITY")
    }
    var c:Cursor? = contentResolver.query(CONTENT_URI, null, null, null, null)
    override fun onCreate(savedInstanceState: Bundle?) {
        if (c!!.count != 0) {
            Log.d("COUNT", "NOT ZERO")
        } else {
            Log.d("COUNT 1", "ZERO")
        }
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

}

But I'm geting an exception like,

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference

Please help me with this.

Upvotes: 2

Views: 656

Answers (1)

DivideBy0
DivideBy0

Reputation: 85

move this line into onCreate function

var c:Cursor? = contentResolver.query(CONTENT_URI, null, null, null, null)

Upvotes: 2

Related Questions