Im developing an app that gathers attendance data and saves to sqlite (offline mode). On a daily basis, I would like the app to post the attendance data to a remote server using Volley. Im successful with posting a single attendance thru Volley upon submit by the user, but I would like the app to post multiple attendances (days) saved in sqlite from a single Volley subclass that is trigger in the onResume() of an activity.
The issue is that I am getting an error with the following codes as I believe I am not correctly initializing the subclass context as Application() - as I'm fairly new with Kotlin development. I have included each part of the Kotlin files that attempts to process the post to remote server.
ATTENDANCEFRAGMENT.KT This is the fragment activity that calls the subclass under onResume()
... private lateinit var dataBase: TransactionDatabase
override fun onResume() {
super.onResume()
dataBase.postTransactions()
}
In my sqlite database class, I created a Select query of all Attendances for this user and within the select loop, I call the Volley singleton subclass to Post the data to the remote server:
TRANSACTIONDATABASE.KT
fun postTransactions() {
val sql = "select * from $TABLE order by $COL_ID desc"
val db = this.readableDatabase
val cursor = db.rawQuery(sql, null)
if (cursor.moveToFirst()) {
do {
val id = cursor.getString(0).toInt()
val uid = cursor.getString(1).toInt()
val clockin = cursor.getString(2)
val clockout = cursor.getString(3)
val customer = cursor.getString(4)
val dateStamp = cursor.getString(5)
apiRequest.postTransactions(uid,clockin,clockout,customer,dateStamp)
}
while (cursor.moveToNext())
}
cursor.close()
}
This is my Volley singleton class:
APIREQUEST.KT
class ApiRequest : Application() {
fun postTransactions(uid: Int, clockin: String, clockout: String, customer: String, dateStamp: String ) {
val requestQueue : RequestQueue = Volley.newRequestQueue(applicationContext)
val url = AppConstant.endpoint_url + "uid=$uid&clockin=$clockin&clockout=$clockout&customer=$customer&dateStamp=$dateStamp"
val request = JsonObjectRequest(Request.Method.GET, url, null, {
response ->try {
val jsonArray = response.getJSONObject("Attendance")
} catch (e: JSONException) {
e.printStackTrace()
}
}, { error -> error.printStackTrace()
})
requestQueue.add(request)
}
The is the error I am getting:
Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
Any help would be greatly appreciated. I also found this online in GitHub that provides an example of a Volley subclass, but I am not able to successfully apply it to my app: https://google.github.io/volley/requestqueue.html
Thanks Octavious