Home > Mobile >  Intent won't open with JavaNullPoint Exception Kotlin
Intent won't open with JavaNullPoint Exception Kotlin

Time:02-27

fun openMovieFromSearch(movieeName: String) {
    val actionArrayList = arrayListOf<String>()
    var targetMoodMoviePressed = "Action"

    val intent = Intent(this, MoviePageActivity::class.java)
    intent.putExtra("Mood Choice", "Aggressive")
    startActivity(intent)

}

It returns with the error in LogCat

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference

The code is working perfectly in another function call, but I don't know why it doesn't work here. Note: I'm calling the openMovieFromSearch() function from another activity

CodePudding user response:

You should never be referencing one activity in another directly. I don't know how you got an instance of ActivityA in ActivityB, but I can assure you that you shouldn't do whatever it is. You're either doing something wrong, or creating a massive memory leak.

Instead, put this function in a separate class or file, and make it take a Context as a parameter rather than using this. Then it can be called as common code.

  • Related