0

Creating an array of buttons

    val buttons = arrayOf(spot0, spot1, spot2, spot3, spot4, spot5, spot6)
    Log.v("array", "spot0=" + buttons[0])

    images.shuffle()
    val randomnumber = (0..5).random()

    buttons[0].setBackgroundResource(images[randomnumber])

Gets this error message:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setBackgroundResource(int)' on a null object reference

and this log output:

V/array: spot0=null

This is Kotlin so I shouldn't have to call findViewById from what I can tell.

apply plugin: 'kotlin-android-extensions' is added to my build.gradle

Button is setup in xml:

<ImageButton
    android:id="@+id/spot0"
    android:text="zero"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="131dp"
    android:layout_marginLeft="131dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="131dp"
    android:layout_marginRight="131dp"
    android:background="@drawable/heart"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.565"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

I'm attempting to randomize my button images and attempting to do it via an array. How can I go about this?

9
  • where are you initializing buttons array? Commented Jun 11, 2020 at 19:53
  • Make sure your code is inside onCreate method. If you try to initialize the buttons array before onCreate then it will cause some problem.. Commented Jun 11, 2020 at 19:53
  • The first usage of the array in my code is what is above, but it didn't copy paste over all the text. I am not initializing it anywhere else. It is in onCreate. The full text is: val buttons :Array<ImageButton!> = arrayOf(spot0, spot1, spot2, spot3, spot4, spot5, spot6) Commented Jun 11, 2020 at 20:00
  • val buttons = arrayOf(spot0, spot1, spot2, spot3, spot4, spot5, spot6) buttons[0] = spot0 Log.v("array", "spot0=" + buttons[0]) That still outputs a null in the log. Commented Jun 11, 2020 at 20:04
  • Do you call setContentView(R.id.your_layout) method before initializing the array? Commented Jun 11, 2020 at 20:06

1 Answer 1

1

If you are in Activity:

You should call setContentView(R.id.your_layout) method in Activity's onCreate() method before accessing the views and initializing the array.

If you are in Fragment:

Try to access the views and initialize the array in onViewCreated() method.

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

4 Comments

I'm in onCreate(). setContentView returns the error: "Unresolved reference: setContentView" setContentView works fine in the MainActivity.kt but it isn't working in this fragment class that I'm working in.
@SiobhanS I've edited my answer. Try to do your things in onViewCreated() method.
Yes! This worked! Thank you!!! I moved the code to onViewCreated() and it worked perfectly.
great. that's because in Fragment's onCreate method the layout is not set and views are not initialized yet.

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.