4

I have an issue with creating buttons dynamically. I used the help provided by How can I dynamically create a button in Android? Although is does help tremendously it isn't exactly working with my specific situation. I am trying to create an array of buttons inside of a scroll view. These buttons will basically be created on the fly based off the answer to the query from a sqlite database. I havent implemented the database as of yet but im just using a for loop with a set variable to create the buttons. I am recieving a Null Pointer Exception when the code ran at this point....

    myButton[index].setText("Button # ");

Here is the code I have been working with for this project.

        @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_aquarium);

    //test creating of dynamic buttons
    Button[] myButton = new Button[4];
    LinearLayout scrViewButLay = (LinearLayout) findViewById(R.id.scrollViewLinLay);
    for(int index = 0; index < 4; index++){
        Log.i("ForTag", "Inside for loop");
        Log.i("ForTag", "button length is "+myButton.length);
        myButton[index].setText("Button # ");//null ptr exception error
        Log.i("ForTag", "After set text");
        scrViewButLay.addView(myButton[index]);
        Log.i("ForTag", "After adding to view");
    }
}

And here is the Null Pointer Exception Error

    10-02 12:07:11.373: D/ActivityThread(16944): setTargetHeapUtilization:0.25
    10-02 12:07:11.373: D/ActivityThread(16944): setTargetHeapIdealFree:8388608
    10-02 12:07:11.373: D/ActivityThread(16944): setTargetHeapConcurrentStart:2097152
    10-02 12:07:11.443: I/ForTag(16944): Inside for loop
    10-02 12:07:11.443: I/ForTag(16944): button length is 4
    10-02 12:07:11.443: W/dalvikvm(16944): threadid=1: thread exiting with uncaught exception         (group=0x41c5a438)
    10-02 12:07:11.443: E/AndroidRuntime(16944): FATAL EXCEPTION: main
    10-02 12:07:11.443: E/AndroidRuntime(16944): java.lang.RuntimeException: Unable to start         activity         ComponentInfo{com.aquariumdatabase.seanrsolution/com.aquariumdatabase.seanrsolution.MainAquariumActiv        ity}: java.lang.NullPointerException
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2073)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2098)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         android.app.ActivityThread.access$600(ActivityThread.java:138)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         android.os.Handler.dispatchMessage(Handler.java:99)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at android.os.Looper.loop(Looper.java:137)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         android.app.ActivityThread.main(ActivityThread.java:4905)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         java.lang.reflect.Method.invokeNative(Native Method)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         java.lang.reflect.Method.invoke(Method.java:511)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at dalvik.system.NativeStart.main(Native         Method)
    10-02 12:07:11.443: E/AndroidRuntime(16944): Caused by: java.lang.NullPointerException
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         com.aquariumdatabase.seanrsolution.MainAquariumActivity.onCreate(MainAquariumActivity.java:23)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         android.app.Activity.performCreate(Activity.java:5240)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2037)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    ... 11 more
0

1 Answer 1

2

You are initializing an array to store your buttons in. You still need to initialize each individual button within that array.

for(int index = 0; index < 4; index++) {
    myButton[index] = new Button(this); //initialize the button here
    myButton[index].setText("Button # "); 
}
Sign up to request clarification or add additional context in comments.

2 Comments

@user2839586 No problem. Make sure to click the checkbox next to my answer to accept it as the answer.
How do you do this without putting a dimension size

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.