Null Pointer Exception when transferring another class's array data from one activity to another

54 Views Asked by At

I am attempting to transfer another class's array data from one activity to another via "Intent.putExtra". My intention is to use the same class for the quiz activity regardless of whether it's on level one or level two by putting specific arrays for different levels when the user selects level one or level two to prevent me from making so many individual classes for each level.

class currentActivity : AppCompatActivity() {

button_levelOne.setOnClickListener{
            val nextPage = Intent(this, nextActivity::class.java)

            nextPage.putExtra("key_questions", backendDataQuestions().questionLevelOne)

            startActivity(nextPage)
            finish()
        }

button_levelTwo.setOnClickListener{
            val nextPage = Intent(this, nextActivity::class.java)

            nextPage.putExtra("key_questions", backendDataQuestions().questionLevelTwo)

            startActivity(nextPage)
            finish()
        }
}

class nextActivity : AppCompatActivity() {
    lateinit var array_questionContents: Array<String>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_game)

        //DOES NOT WORK!!!
        array_questionContents = getIntent().getSerializableExtra("key_questions") as Array<String>

}
class backendDataQuestions(){
val questionLevelOne = arrayOf(
        "Question 1",
        "Question 2",
        "Question 3"
    )

val questionLevelTwo = arrayOf(
        "Question 1",
        "Question 2",
        "Question 3"
   )
}

This results in this error message:

"java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.application/com.example.application.nextActivity}: java.lang.NullPointerException: null cannot be cast to non-null type kotlin.Array<kotlin.String>"

I have already tried to make the lateinit var array_questionContents: Array<String> into an Array<Any> and into an Array<*> to no avail.

I have previously made the "nextActivity" class call the backendDataQuestions class's array directly when required and it worked fine.

1

There are 1 best solutions below

0
Johnny Alpha On BEST ANSWER

The issue you're facing is likely due to how you're initializing the backendDataQuestions class instances. Every time you call backendDataQuestions(), you're creating a new instance of the class, which means the array you're trying to access in nextActivity is not the same one you put in the intent extras.

To fix this, you should create an instance of backendDataQuestions and store it in the intent extras, rather than creating a new instance each time you set the extras. Here's how you can modify your code:

button_levelOne.setOnClickListener {
    val nextPage = Intent(this, nextActivity::class.java)
    val backendQuestions = backendDataQuestions()

    nextPage.putExtra("key_questions", backendQuestions.questionLevelOne)
    startActivity(nextPage)
    finish()
}

button_levelTwo.setOnClickListener {
    val nextPage = Intent(this, nextActivity::class.java)
    val backendQuestions = backendDataQuestions()

    nextPage.putExtra("key_questions", backendQuestions.questionLevelTwo)
    startActivity(nextPage)
    finish()
}

And in your nextActivity:

class nextActivity : AppCompatActivity() {
    lateinit var array_questionContents: Array<String>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_game)

        array_questionContents = intent.getStringArrayExtra("key_questions") ?: emptyArray()
    }
}

By creating only one instance of backendDataQuestions and using that instance to access your arrays, you ensure that the same arrays are being referenced both when putting them into the intent extras and when retrieving them in the nextActivity.