New to the forums and could use a hand. I am completely stuck, and not sure what to do at this point.

I keep getting "TRACE: <at java.lang.IndexOutOfBoundsException: Empty field: 106>, Exception caught in Display class java.lang.IndexOutOfBoundsException: Empty field: 106" during run-time when the method, displayList() is called (currently on a softkey for testing purposes).

The Netbeans output window mentions this line, "String [] nameValues = c.getStringArray(Contact.NAME, 0);, located in displayList(). Maybe there is something wrong with this, or Netbeans is just pointing to it for the heck of it =P.

// packages
import javax.microedition.midlet.*; // for midlet life cycle
import javax.microedition.lcdui.*; // for user interface components
import java.util.*; // for dates
import javax.microedition.pim.*; // for record store
import java.io.*;

// this is the midlet
public class ContactsMidlet extends MIDlet implements CommandListener {
    // private data fields
    // here goes commands, display, forms, database, etc

    private List contactList;
    private Form contactAddForm;
    private Form contactEditForm;

    private Command exitCommand;
    private Command screenCommand;
    private Command backCommand;
    private Command okCommand;




    // midlet constructor
    public ContactsMidlet() {
        /* create display, commands, and screens
        * add commands to screens
        * open filesystem
        * display editor files (e.g., .txt)
        */
        try{
            verifyPIMSupport();
            //seed();
        }
        catch (Exception ex){}

        //Create Forms
        contactList = new List("Contact List", 3);
        contactAddForm = new Form("Add Form");
        contactEditForm = new Form("Edit Form");

        //Create Comands
        exitCommand = new Command("Exit", Command.EXIT, 0);
        screenCommand = new Command("New", Command.SCREEN, 0);
        backCommand = new Command("Back", Command.BACK, 0);
        okCommand = new Command ("Okay", Command.OK, 0);


        //Add Commands to appropriate forms
        contactList.addCommand(exitCommand);
        contactList.addCommand(screenCommand);

        contactAddForm.addCommand(backCommand);
        contactAddForm.addCommand(okCommand);

        contactEditForm.addCommand(backCommand);


        //Set Command Listners
        contactList.setCommandListener(this);
        contactAddForm.setCommandListener(this);
        contactEditForm.setCommandListener(this);

    } // end of constructor

    // start the midlet
    public void startApp() {
        Display.getDisplay(this).setCurrent(contactList);

    } // end of startApp

    // pause midlet
    public void pauseApp() {
    } // end of pauseApp

    // destroy midlet
    public void destroyApp(boolean unconditional) {
        // close any open system files if any
    } // end of destroyApp

    // process commands
    public void commandAction(Command c, Displayable d) {
        // process the action of each command
        // (exit, back, save, add, delete, etc…)
        if (d == contactList && c== screenCommand){
                try{
                    seed();
                }
                catch(Exception ex){}
                    Display.getDisplay(this).setCurrent(contactAddForm);

        }else if (d == contactAddForm && c == backCommand){
                displayList();
                Display.getDisplay(this).setCurrent(contactList);
        }


    }

    public void verifyPIMSupport() throws IOException {
        String version = "";
        version = System.getProperty("microedition.pim.version");
        if (version != null){
            if(!version.equals("1.0"))
                throw new IOException("Package is not version 1.0");

        }
        else
            throw new IOException("PIM optional package is not available");
    }


    private ContactList list = null;

    private void seed() throws PIMException {
        try{
            PIM pim = PIM.getInstance();
            list = (ContactList) pim.openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE);
        }
        catch (PIMException ex){}
        addContact(list, "Ray", "Rischapter", "1234 High Street",
                   "Los Gatos", "USA", "95030");
        addContact(list, "John", "Doe", "1111 Bear Road",
                   "Mariposa", "USA", "98342");

        if (list != null)
            list.close();
        list = null;
    }

    private void displayList(){
        
        try{
            PIM pim = PIM.getInstance();
            ContactList contList = (ContactList)
                pim.openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY);
            Enumeration contacts = contList.items();
            
            while (contacts != null && contacts.hasMoreElements()){
                Contact c = (Contact) contacts.nextElement();
                String [] nameValues = c.getStringArray(Contact.NAME, 0);
                String firstName = nameValues[Contact.NAME_GIVEN];
                String lastName = nameValues[Contact.NAME_FAMILY];
                contactList.append(lastName + ", " + firstName, null);
                
            }

        }
        catch (PIMException ex){}
        


    }

    private void addContact(ContactList list, String firstName,
                            String lastName, String street, String city,
                            String country, String postalcode)
    throws PIMException{
        Contact c = list.createContact();
        String [] name = new String [list.stringArraySize(Contact.NAME)];
        name[Contact.NAME_GIVEN] = firstName;
        name[Contact.NAME_FAMILY] = lastName;
        c.addStringArray(Contact.NAME, Contact.ATTR_NONE, name);
        String [] addr = new String[list.stringArraySize(Contact.ADDR)];
        addr[Contact.ADDR_STREET] = street;
        addr[Contact.ADDR_LOCALITY] = city;
        addr[Contact.ADDR_COUNTRY] = country;
        addr[Contact.ADDR_POSTALCODE] = postalcode;
        c.addStringArray(Contact.ADDR, Contact.ATTR_NONE, addr);
        c.commit();
        
    }

} // end of ContactsMidlet

Please, if there is anything else I can provide to help gain assistance just let me know. This is an assignment, and I am not looking for hand outs, just a push in the right direction. Thanks for any advice.

IndexOutOfBoundsException typically means that you attempted to access an index of an array, but the index you tried to access was greater than the highest index of the array. So somewhere in the getStringArray method this is happening, although you didn't post that method's code.

IndexOutOfBoundsException typically means that you attempted to access an index of an array, but the index you tried to access was greater than the highest index of the array. So somewhere in the getStringArray method this is happening, although you didn't post that method's code.

First, thanks for the reply.

The getStringArray, from my text, is a PIMItem accessor method.

getStringArray: Returns the contents of a string array field as a String [] given its field key and the index of the nth datum of that type.

In the addContact method, an array of strings, containing the first and last name, is added to the contact before a commit of the new contact.

Is that the full error trace you got? I would like to see the whole error tract list. The line mention in your error does not match with the code you posted. It is very difficult to pinpoint where the bug is.

By the way, is your Enumeration a class or an interface? Check here if yours is an interface.

Here is a full error trace I get from Netbeans when the displayList() method is called.

TRACE: <at java.lang.IndexOutOfBoundsException: Empty field: 106>, Exception caught in Display class
java.lang.IndexOutOfBoundsException: Empty field: 106
at com.sun.j2me.pim.AbstractPIMItem.getValue(), bci=36
at com.sun.j2me.pim.AbstractPIMItem.getStringArray(), bci=9
at ContactsMidlet.displayList(ContactsMidlet.java:142)
at ContactsMidlet.commandAction(ContactsMidlet.java:94)
at javax.microedition.lcdui.Display$ChameleonTunnel.callScreenListener(), bci=46
at com.sun.midp.chameleon.layers.SoftButtonLayer.processCommand(), bci=74
at com.sun.midp.chameleon.layers.SoftButtonLayer.soft1(), bci=37
at com.sun.midp.chameleon.layers.SoftButtonLayer.keyInput(), bci=36
at com.sun.midp.chameleon.CWindow.keyInput(), bci=38
at javax.microedition.lcdui.Display$DisplayEventConsumerImpl.handleKeyEvent(), bci=17
at com.sun.midp.lcdui.DisplayEventListener.process(), bci=277
at com.sun.midp.events.EventQueue.run(), bci=179
at java.lang.Thread.run(Thread.java:619)


As for the second question, from my current understanding, the items method used in line 138, returns an Enumeration of the contents of the PIMList (contList). Looking at the API, I was under the assumption that this

http://download.oracle.com/javame/config/cldc/ref-impl/midp2.0/jsr118/java/util/Enumeration.html

is what is being used.

Hmm... You are giving us wrong class code. The error states that something is wrong in "Display class" at line 106. Why are you convinced that the error is from ContactsMidlet class line 142 (not 138)? Something could be wrong when the class accepts argument from ContactsMidlet?

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.