title: Null Pointer Exception in Android while fetching data from SQLite database
body: <p>I am trying to get a list of songs marked as favorite and display in the favorite fragment. however the app is crashing every time I try to open the favorite screen. I have made the database object static in MainActivity(holds all the fragments)</p>
  <p>MainActivity.Kt</p>
  <pre><code>class MainActivity : AppCompatActivity() {
  
      var drawerLayout: DrawerLayout?=null
  
      object static{
          var favData : EchoDatabase?=null
      }
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
          favData = EchoDatabase(this)
          val homeFragment = HomeFragment()
          this.supportFragmentManager
              .beginTransaction()
              .add(R.id.fragmentContainer,homeFragment,&quot;HomeFragment&quot;)
              .commit()
          initializedrawerlayout()
          initialiseRecyclerView(this)
      }
  </code></pre>
  <p>SongPlaying.kt (The favourite Songs are sent from here)</p>
  <pre><code>favbutton?.setOnClickListener {
              if(fav)
              {
                  favbutton?.setBackgroundResource(R.drawable.favorite_off)
                  favData?.deleteSongs(song!![position].songID)
                  fav=false
              }
              else
              {
                  fav=true
                  favbutton?.setBackgroundResource(R.drawable.favorite_on)
                  favData?.storeSongs(song!![position])
  
              }
          }
  </code></pre>
  <p>Favourites.Kt</p>
  <pre><code>package com.example.echo.Fragments
  
  import android.app.Activity
  import android.content.Context
  import android.os.Bundle
  import android.support.v4.app.Fragment
  import android.support.v7.widget.LinearLayoutManager
  import android.support.v7.widget.RecyclerView
  import android.view.LayoutInflater
  import android.view.View
  import android.view.ViewGroup
  import android.widget.ImageButton
  import android.widget.RelativeLayout
  import android.widget.TextView
  import com.example.echo.Adapter.SongListAdapter
  import com.example.echo.DataModels.Songs
  import com.example.echo.Database.EchoDatabase
  import com.example.echo.MainActivity
  import com.example.echo.MainActivity.static.favData
  import com.example.echo.R
  
  class Favourites : Fragment() {
  
      var mcontext : Activity?=null
      var favsonglistRV : RecyclerView?=null
      var favsonglistVisibleLayout: RelativeLayout?=null
      var favnoSongs : RelativeLayout?=null
  
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
      }
  
      override fun onCreateView(
          inflater: LayoutInflater,
          container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
          var view = inflater.inflate(R.layout.fragment_favourites,container,false)
          favsonglistRV = view.findViewById&lt;RecyclerView&gt;(R.id.fav_songsRV)
          favsonglistVisibleLayout = view.findViewById&lt;RelativeLayout&gt;(R.id.fav_songslist_visible_layouyt)
          favnoSongs = view.findViewById&lt;RelativeLayout&gt;(R.id.fav_nosongs)
          return view
      }
  
      override fun onAttach(context: Context?) {
          super.onAttach(context)
          mcontext =context as Activity
      }
  
      override fun onAttach(activity: Activity?) {
          super.onAttach(activity)
          mcontext =activity
      }
  
      override fun onActivityCreated(savedInstanceState: Bundle?) {
          super.onActivityCreated(savedInstanceState)
  
          if(MainActivity.static.favData!!.checkifdata() &amp;&amp; MainActivity.static.favData!!.getsongs()!=null)
          {
              favsonglistVisibleLayout?.visibility = View.VISIBLE
              favnoSongs?.visibility = View.INVISIBLE
              intitialiseRV(favData!!.getsongs(),mcontext as Context)
          }
          else
          {
              favsonglistVisibleLayout?.visibility = View.INVISIBLE
              favnoSongs?.visibility = View.VISIBLE
          }
      }
  
      fun intitialiseRV(songs: ArrayList&lt;Songs&gt;, context: Context)
      {
          favsonglistRV?.layoutManager = LinearLayoutManager(context)
          var adapter = SongListAdapter(songs,context)
          favsonglistRV?.adapter =adapter
      }
  }
  </code></pre>
  <p>EchoDatabase.KT</p>
  <pre><code>package com.example.echo.Database
  
  import android.content.ContentValues
  import android.content.Context
  import android.database.sqlite.SQLiteDatabase
  import android.database.sqlite.SQLiteOpenHelper
  import com.example.echo.DataModels.Songs
  import com.example.echo.Database.EchoDatabase.static.db_name
  import java.lang.Exception
  
  class EchoDatabase : SQLiteOpenHelper {
      object static
      {
          val db_name =&quot;fav_song_db&quot;
      }
  
      val tname = &quot;fav_song_list&quot;
      val _id =&quot;song_ID&quot;
      val title =&quot;song_title&quot;
      val artist = &quot;song_artist&quot;
      val path = &quot;song_path&quot;
  
      constructor(
          context: Context?,
          name: String?,
          factory: SQLiteDatabase.CursorFactory?,
          version: Int
      ) : super(context, name, factory, version)
  
      constructor(context: Context?) : super(context,db_name,null,1)
  
      override fun onCreate(db: SQLiteDatabase?) {
          var query = &quot;CREATE TABLE $tname($_id LONG,$title STRING,$artist STRING,$path STRING );&quot;
          db?.execSQL(query)
      }
  
      override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
          TODO(&quot;Not yet implemented&quot;)
      }
  
      fun storeSongs (i : Songs)
      {
          val db = this.writableDatabase
          var contentValues = ContentValues()
          contentValues.put(_id,i.songID)
          contentValues.put(title,i.songTitle)
          contentValues.put(artist,i.songArtist)
          contentValues.put(path,i.songData)
          db.insert(tname,null,contentValues)
          db.close()
      }
  
      fun checkSong(songID: Long): Boolean {
          val db = this.readableDatabase
          var query = &quot;SELECT * FROM $tname WHERE $_id = $songID;&quot;
          var cursor = db.rawQuery(query,null)
          try {
              if(cursor.moveToFirst())
              {
                  db.close()
                  return true
              }
          }
          catch (e: Exception)
          {
              e.printStackTrace()
          }
          db.close()
          return false
      }
  
      fun deleteSongs(songs: Long) {
          val db = this.writableDatabase
          db.delete(tname, &quot;$_id=$songs&quot;, null)
          db.close()
      }
  
      fun checkifdata (): Boolean
      {
          val db = this.readableDatabase
          var query = &quot;SELECT * FROM $tname&quot;
          var cursor = db.rawQuery(query,null)
          try {
              if(cursor.moveToFirst())
              {
                  db.close()
                  return true
              }
          }
          catch (e: Exception)
          {
              e.printStackTrace()
          }
          db.close()
          return false
      }
  
      fun getsongs(): ArrayList&lt;Songs&gt;
      {
          var favlist: ArrayList&lt;Songs&gt;?=null
          val db = this.readableDatabase
          var query = &quot;SELECT * FROM $tname&quot;
          var cursor = db.rawQuery(query,null)
          try {
              if(cursor.moveToFirst())
              {
  
              }
          }
          catch (e: Exception)
          {
              e.printStackTrace()
          }
          return favlist!!
      }
  
  }
  </code></pre>