0

I want to directly initialize an array of buttons without having to specify variable names. I thought I found the answear here: Array of buttons in Kotlin, but the answer throws a NullPointerException. I have also searched google for 'array of buttons in kotlin' but the only relevant info I found was from the question I linked.

I used

val intervalButtons = arrayOf(
        findViewById<Button>(R.id.set30secButton),
        findViewById<Button>(R.id.set60secButton),
        findViewById<Button>(R.id.set90secButton),
        findViewById<Button>(R.id.set120secButton)
    )

However I also tried to apply plugin: 'kotlin-android-extensions' in the build.gradle file and just using

val intervalButtons = arrayOf(set30secButton, set60secButton, set90secButton, set120secButton)

but this still throws a NullPointerException.

If I use

        btn1 = findViewById<Button>(R.id.set30secButton)

it works like a charm, but like I said, I don't want to have to specify every variable name if I don't have to.

1
  • Yes, it would throw null pointer if you declared the array at class level, do it locally or use lateinit or lazy, you should init after the view inflated, in activity after setContentView and in fragment in onViewCreated. Commented Jan 20, 2021 at 15:30

1 Answer 1

0

You might be putting the code in the wrong place. You didn't specify that, but it's either in the class itself (in which case findViewById would most likely return null) or in onCreate - in which case it would not be visible outside this function

Either way, the correct way either be:

...
private lateinit var intervalButtons: Array<Button>
...
override fun onCreate(savedInstanceState: Bundle?) {
    setContentView(R.layout.your_view) // <- this is important, must be before     findViewById
    intervalButtons = arrayOf(
        findViewById<Button>(R.id.set30secButton),
        findViewById<Button>(R.id.set60secButton),
        findViewById<Button>(R.id.set90secButton),
        findViewById<Button>(R.id.set120secButton)
    )
    ...
}

lateinit var means that the variable will be initialized later and as such doesn't need to be initialized at once

Or like such:

val intervalButtons: Array<Button> by lazy {
    arrayOf(
        findViewById<Button>(R.id.set30secButton),
        findViewById<Button>(R.id.set60secButton),
        findViewById<Button>(R.id.set90secButton),
        findViewById<Button>(R.id.set120secButton)
    )
}
...
override fun onCreate...

Here by lazy means the variable intervalButtons will only be initialized when it's needed - so like when you'd tried to access one of the buttons. In both solutions findViewById is called after setContentView, which might be the issue in your case.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I placed the code at the start of the class. Placing it in the onCreate function solved it, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.