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.