Question

I have XML being parsed into a custom blogger class array, with this I am trying to show on screen a ChoiceGroup of the .name attribute of my blogger class.

for(int i = 0; i< intBloggerLength; i++)
    {
        menuItems[i] = myXMLParser.arrBloggers[i].getName();
        //lstView.append(myXMLParser.arrBloggers[i].getName().toString(), null);
    }
String strPause = "";
grpMenuItems = new ChoiceGroup("Bloggers", Choice.IMPLICIT, menuItems,
       new Image[] {null, null, null, null, null, null});

"grpMenuItems =..." is the line that is erroring, and making my MIDlet crash, I have been following examples online and I am populating the ChoiceGroup the same way, does anyone have any ideas?

Was it helpful?

Solution

The issue was me using a Choice.IMPLICIT which does not exist for ChoiceGroup, but for List.

Changing for Choice.EXCLUSIVE works fine.

OTHER TIPS

Your main mistake here is I think not using appropriate logging in your MIDlet. That makes debugging of the issues like you describe unnecessary complicated. mistakes in the code snippet you posted.

  • With logging done right (refer here for details if you're interested), you could simply run your midlet in emulator and check console messages to find out whether expected code.

From the code snippet you posted and your explanations it looks like intBloggerLength value is not constant. Coupled with the way how you invoke ChoiceGroup constructor in the line you mention as problematic, this looks a sure way to get IllegalArgumentException.

  • You could find that out yourself if you'd wrap this line into try-catch and put appropriate logging code into catch block but since you didn't do that, we have to merely guess. Note if you test in emulator, there is also a chance for exception message and stack trace to be shown in its console.

To find out how you could get that exception, refer to API javadocs for the constructor you use:

Throws:
...IllegalArgumentException - if the imageElements array is non-null and has a different length from the stringElements array...

There are three other possible reasons for exception to be thrown listed in API docs, but I'll focus on one where your code snippet looks very slippery.

Let's look back at your code, images array length is 6: {null, null, null, null, null, null} - this means as soon as intBloggerLength differs from 6, this will throw IAE.

To fix this, simplest way would be to pass null value instead of images array. Or, if you need the array to be not null, just write the code so that it has valid length, eg

new Image[menuItems.length]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top