开发者

BlackBerry: creating a dynamic no. of buttons based on the contents of a vector

开发者 https://www.devze.com 2022-12-12 12:51 出处:网络
based on the contents of a vector(IDs), i\'m trying to create the corresponding no. of buttons but I\'m having problems doing that. I was wondering if anyone could help?

based on the contents of a vector(IDs), i'm trying to create the corresponding no. of buttons but I'm having problems doing that. I was wondering if anyone could help?

Below is the code that i'm using to try to get that done...

ButtonField[] btn = new ButtonField[list.IDs.size()];
        for(int i=0; i&开发者_如何学Golt;list.IDs.size(); i++){
            btn[i].setLabel((String)list.IDs.elementAt(i));
            add(btn[i]);
        }

I'm currently getting an null pointer exception on the setLabel line.


You are creating an array (btn) of ButtonFields but you aren't actually initializing the actual elements of the array.

When you create an array in Java, all of the elements of the array are initially NULL.

Try this:

ButtonField[] btn = new ButtonField[list.IDs.size()];
        for(int i=0; i<list.IDs.size(); i++){
            btn[i] = new ButtonField(...);
            btn[i].setLabel((String)list.IDs.elementAt(i));
            add(btn[i]);
        }

Notice the new line, which sets the array element to an actual object:

btn[i] = new ButtonField(...);

Of course, you will need to fill in whatever arguments are necessary for the constructor.

0

精彩评论

暂无评论...
验证码 换一张
取 消