return - kotlin function returning null -


i trying android development kotlin. in case want overwrite: contentprovider have overwrite function "query". "query" returns "cursor" type. however, when create cursor instance in function database.query "cursor?" type. can return cursor if not null, do if null?

this looks like:

override fun query(uri: uri, projection: array<out string>?, selection: string?, selectionargs: array<out string>?, sortorder: string?): cursor {      val cursor = querybuilder.query(db, projection, selection, selectionargs, null, null, sortorder)             // make sure potential listeners getting notified             cursor?.setnotificationuri(getcontext()?.getcontentresolver(), uri)              if(cursor != null)                 return cursor             else                 // here? 

any ideas how solve that?

thanks, sven

update first of answers.

annotation seems not work in case can access compiled code , dont option annotate source. maybe missing here.

implementing own cursor seems overkill if have every time problem occurs.

so seems option return cursor!! dont know how it. code bit more complicated in example, missed when statement. updated version:

override fun query(uri: uri, projection: array<out string>?, selection: string?, selectionargs: array<out string>?, sortorder: string?): cursor {      val cursor = ??? //how initialize cursor somehow?     val db = database.getwritabledatabase()      if(db != null) {         val cursor = querybuilder.query(db, projection, selection, selectionargs, null, null, sortorder)                 // make sure potential listeners getting notified                 cursor?.setnotificationuri(getcontext()?.getcontentresolver(), uri)                  if(cursor != null)                     return cursor      }      // return here? cursor not exist } 

do have implement own cursor , clutter code useless "throw unsupportedexceptions"?

your options are:

  • if cursor null, throw exception: return cursor!! (this ok in case sure cursor never null
  • if cursor null, return trivial cursor (e.g. own implementation of abstractcursor behaves empty result set
  • (as franck said in comment) annotate querybuilder.query() function @notnull: http://blog.jetbrains.com/kotlin/using-external-annotations/

Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -