0

Hi,
I encounter the problem of not being able to read the information from the contact list.
It is ok to read organization name and email. But I can not read the name information.
I get the exception message of
java.lang.IndexOutOfBoundsException: Empty field: 106>, Exception caught in Display class
What is the problem? At first, I read the org information and show that information in the list. When the user chooses one organization name from the list, it will show the name associated with that organization.
I think there is error not only in the part of reading name information, but also in part of showing that data because it shows the following exception message when I just try to display the email information .
TRACE: <at java.lang.NullPointerException: 0>, Exception caught in Display class
java.lang.NullPointerException: 0


//ZSecondContactMIDlet .java

package zthirdcontact;
import java.util.Enumeration;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.pim.*;


public class ZSecondContactMIDlet extends MIDlet implements CommandListener{
	static ZSecondContactMIDlet zsecondcontactmidlet;
	Form mainForm;
	StringItem stItem;
	Display display;
	Command cmdExit;
	Command cmdRead;
	Command cmdDelete;
	Command cmdWrite;
	
	

	public ZSecondContactMIDlet() {
		mainForm=new Form("Contact Program");
		stItem=new StringItem("*******","Data has been entered");
		cmdExit=new Command("Exit",Command.EXIT,0);
		cmdRead=new Command("Read",Command.SCREEN,0);
		cmdDelete=new Command("Delete",Command.SCREEN,0);
		cmdWrite=new Command("Write",Command.SCREEN,0);
		
		
		mainForm.append(stItem);
		mainForm.addCommand(cmdRead);
		mainForm.addCommand(cmdExit);
		mainForm.addCommand(cmdDelete);
		mainForm.addCommand(cmdWrite);
		mainForm.setCommandListener(this);
		display=Display.getDisplay(this);
		Verify();
		
		
		
		
	}

	
	private void Verify() {
		String version=System.getProperty("microedition.pim.version");
		if(version==null)
		{
			System.out.println("PIM is not available");
		}
		
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	protected void startApp() throws MIDletStateChangeException {
		
		display.setCurrent(mainForm);

	}

	public void commandAction(Command c, Displayable d) {
		
		
		if(c==cmdExit)
		{
			try {
				destroyApp(true);
				notifyDestroyed();
			} catch (MIDletStateChangeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(c==cmdWrite)
		{
			
				PIM pim=PIM.getInstance();
				ContactList contactlist=null;
				Contact contact=null;
				try {
					contactlist=(ContactList)pim.openPIMList(PIM.CONTACT_LIST,PIM.WRITE_ONLY);
					contact=contactlist.createContact();
					String[]name_struct=new String[contactlist.stringArraySize(Contact.NAME)];
					name_struct[Contact.NAME_GIVEN]="Rose";
					name_struct[Contact.NAME_FAMILY]="Angel";
					contact.addString(Contact.ORG, PIMItem.ATTR_NONE, "Microsoft");			
					contact.addString(Contact.TEL, PIMItem.ATTR_NONE, "099045464");
					contact.addString(Contact.EMAIL, PIMItem.ATTR_NONE, "rose@gmail.com");
					contact.commit();
				} catch (PIMException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				if(contactlist!=null)
				{
					try {
						contactlist.close();
						contactlist=null;
					} catch (PIMException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
		}//if(c==cmdWrite)
		
		if(c==cmdRead)
		{
			List listName;
			PIM pim=PIM.getInstance();
			ContactList contactlist=null;
			Enumeration contacts=null;
			
			listName=new List("Name List",List.IMPLICIT);
			listName.setCommandListener(new ListCommandHandler());
			
			
			try {				
				contactlist=(ContactList)pim.openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY);
				
				Contact temp_contact=null;
				temp_contact=contactlist.createContact();
				temp_contact.addString(Contact.ORG, PIMItem.ATTR_NONE, "M");				
				contacts=contactlist.items(temp_contact);				
				if(contacts==null)
					{
					System.out.println("There is no PIM record");
					return;
					}				
				while(contacts.hasMoreElements())
				{
					Contact contact=(Contact)contacts.nextElement();					
					String Orgname=contact.getString(Contact.ORG, 0);					
					listName.append(Orgname, null);
					
				}			
			} catch (PIMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			if(contactlist!=null)
			{
				try {
					contactlist.close();
					contactlist=null;
				} catch (PIMException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			display=Display.getDisplay(this);
			display.setCurrent(listName);
			
		}//if(c==cmdRead)
		
		if(c==cmdDelete)
		{
			PIM pim=PIM.getInstance();
			try {
				ContactList contactlist=(ContactList)pim.openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE);
				Enumeration contact_temp=contactlist.items();
				if(contact_temp==null)
				{
					System.out.println("there is no record to delete");
					return;
				}
				
				while(contact_temp.hasMoreElements())
				{
					contactlist.removeContact((Contact)contact_temp.nextElement());
					
				}//while
			} catch (PIMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}//if(c==cmdDelete)	
		
		
	}

}

//ListCommandHandler.java

package zthirdcontact;

//import java.nio.ShortBuffer;
import java.util.Enumeration;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.pim.Contact;
import javax.microedition.pim.ContactList;
import javax.microedition.pim.PIM;
import javax.microedition.pim.PIMException;


public class ListCommandHandler implements CommandListener  {

	public ListCommandHandler() {
		// TODO Auto-generated constructor stub
	}

	public void commandAction(Command c, Displayable d) {
		List listName=(List)d;
		
		
		
		if(c==List.SELECT_COMMAND)
		{			
			try {
				StringBuffer buffer=new StringBuffer();
				int index=listName.getSelectedIndex();
				PIM pim=PIM.getInstance();
				ContactList contactlist=(ContactList)pim.openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE);
				Enumeration contact_temp=null;
				contact_temp=contactlist.items(listName.getString(index));
				
				while(contact_temp.hasMoreElements())
				{
					
					Contact contact=(Contact) contact_temp.nextElement();
					
					String[] name_struct=contact.getStringArray(Contact.NAME, 0);					
					String firstname=name_struct[Contact.NAME_GIVEN];					
					String lastname=name_struct[Contact.NAME_FAMILY];					
					buffer.append("First Name:");
					buffer.append(firstname);
					buffer.append("\n");
					buffer.append("Last Name");
					buffer.append(lastname);
					buffer.append("\n");					
					String email=contact.getString(Contact.EMAIL,0);
					buffer.append(email);
					
					
				}//while
				System.out.println(buffer.toString());
				Alert alertdetail=new Alert("",buffer.toString(),null,AlertType.INFO);
				Display display=Display.getDisplay(ZSecondContactMIDlet.zsecondcontactmidlet);
				display.setCurrent(alertdetail);
				
			} catch (PIMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
			
		}//if(c==listName.SELECT_COMMAND)  

	}

}
2
Contributors
2
Replies
4
Views
8 Years
Discussion Span
Last Post by bluebird
0

Hi,
I have found the error.
I forgot to write this line when the contact name information is written.
contact.addStringArray(Contact.NAME, PIMItem.ATTR_NONE, name_struct);

Thanks for pointing out a good extensive tutorial .

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.