```
  val dialog = AlertDialog.Builder(activity as Context)
    dialog.setTitle("Confirmation")
    dialog.setMessage("Are you sure you want to log out?")
    dialog.setPositiveButton("YES"){ text, listener ->
        sharedPreferences.edit().clear().apply()
        println("Shared preferences cleared")
        println("Logged out")
        val intent =  Intent(activity as Context, LoginActivity::class.java)
        startActivity(intent)
    }
    dialog.setNegativeButton("NO"){text,listener ->

        val homeFragment = HomeFragment()
        val beginTransaction = activity!!.supportFragmentManager.beginTransaction()

        beginTransaction.replace(R.id.frameLayout, homeFragment)
        beginTransaction.commit()
        activity!!.navigationView.setCheckedItem(R.id.home_screen)
        println("Home title set")
        activity!!.actionBar!!.title = "Home"

    }
    dialog.create()
    dialog.show()

```

I want to go the Home Fragment when the user clicks the "No" Button. I am being directed to the Home Fragment but the dialog box does not disappear and when I click no again, the app crashes giving a Null Pointer Exception on the line :

```
val beginTransaction = activity!!.supportFragmentManager.beginTransaction()

```

How can I remove the dialog box when I reach the home fragment?
Please help.