Hey guys,

I'm coding a phonebook viewer using J2ME, now the following code appears to break when the hasMoreElements gets fired! The exception is a arrayoverflow type on. Not sure why though

/*
 * Midlet.java
 *
 * Created on 15 July 2009, 23:38
 */

import java.util.Enumeration;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.pim.*;
/**
 *
 * @author  John
 * @version
 */
public class Midlet extends MIDlet {
    public void startApp() {
        
        PIM pimSingleton = PIM.getInstance();
        ContactList CL=null;
        try {
            CL = (ContactList) pimSingleton.openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY);
        } catch (PIMException ex) {
            ex.printStackTrace();
        }
        Enumeration en=null;;
        try {
            en = CL.items();
        } catch (PIMException ex) {
            ex.printStackTrace();
        }
        if(en.hasMoreElements()) {
            Contact t = (Contact) en.nextElement();
            String telephone = t.getString(Contact.TEL, 0);
            Alert splashScreen = new Alert(null,telephone, null,AlertType.INFO);
            splashScreen.setTimeout(Alert.FOREVER);
            Display.getDisplay(this).setCurrent(splashScreen);
            int i =1;
            while(en.hasMoreElements()) {
                splashScreen = new Alert(null,Integer.toString(i++), null,AlertType.INFO);
                splashScreen.setTimeout(Alert.FOREVER);
                Display.getDisplay(this).setCurrent(splashScreen);
            }
        }
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
}

Could anyone point me in a direction? Or perhaps offer a cure?

Acidburn,
Use if statement instead of while.

if(en.hasMoreElements()) {
                splashScreen = new Alert(null,Integer.toString(i++), 
                null,AlertType.INFO);
                splashScreen.setTimeout(Alert.FOREVER);
                Display.getDisplay(this).setCurrent(splashScreen);
            }

But I'd like to iterate through all the elements in the enum list.

while(en.hasMoreElements()) {
            Contact t = (Contact) en.nextElement();
            String telephone = t.getString(Contact.TEL, 0);
            Alert splashScreen = new Alert(null,telephone, null,AlertType.INFO);
            splashScreen.setTimeout(Alert.FOREVER);
            Display.getDisplay(this).setCurrent(splashScreen);
            int i =1;
            if(en.hasMoreElements()) {
                splashScreen = new Alert(null,Integer.toString(i++), null,AlertType.INFO);
                splashScreen.setTimeout(Alert.FOREVER);
                Display.getDisplay(this).setCurrent(splashScreen);
            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.