Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
PaoloLim
Posts: 21
Registered: ‎07-16-2008
My Device: Not Specified
Accepted Solution

IllegalArgumentException from PIMItem.getString(int, int)

[ Edited ]

int[] intarray = cont.getFields(); for(int x = 0; x < intarray.length; x++) { System.out.println(cont.getString(intarray[x],0)); }

 

 

That is just a sample code, but it throws the same exception that my actual app is.  cont is a Contact object that I initialized from the context object in the addressbook.  Whenever I get to the getString line, it throws an IllegalArgumentException for anything other than the PIMItem.TEL constant despite the fact that the other objects are not null.  I am developing for the Blackberry 7290 with Handheld Software version 4.1 and JDE version 4.1.  Is there any reason I am getting that exception? 

 

EDIT: Sorry, Contact.TEL not PIMItem

Message Edited by PaoloLim on 07-22-2008 02:33 PM
Please use plain text.
Developer
richard_puckett
Posts: 191
Registered: ‎04-03-2008
My Device: Bold 9700

Re: IllegalArgumentException from PIMItem.getString(int, int)

You only want to read it as a String if it *is* a String.  Try something like:

 

 

int[] intarray = cont.getFields(); for(int x = 0; x < intarray.length; x++) { if (myPimList.getFieldDataType(intarray[x]) == PIMItem.STRING) { System.out.println(cont.getString(intarray[x],0)); } }

 


 

 

 

Please use plain text.
Developer
PaoloLim
Posts: 21
Registered: ‎07-16-2008
My Device: Not Specified

Re: IllegalArgumentException from PIMItem.getString(int, int)

Wow it works perfectly! Thank you so much!
Please use plain text.
Developer
PaoloLim
Posts: 21
Registered: ‎07-16-2008
My Device: Not Specified

Re: IllegalArgumentException from PIMItem.getString(int, int)

Follow up:  Well, I have populated a contact via the native address book app to test it out.  When I come up to this code, only the fields 115, 117, and 20000927(.TEL, .UID, and what appears to be the pin though there's no constant for it) get returned as strings.  I've inspected the contact object and all of the values are stored there(name, e-mail, and other numbers), however they do not seem to get returned as strings.  Is there any reason for this?

Please use plain text.
Developer
richard_puckett
Posts: 191
Registered: ‎04-03-2008
My Device: Bold 9700

Re: IllegalArgumentException from PIMItem.getString(int, int)

Contacts can be tricky to work with.  Basically, in addition to STRING, fields can have other values as well, such as STRING_ARRAY (and STRING isn't just a simple string - it can be something like an array of strings each with their own attribute).  Is your head spinning yet?  :smileyhappy:

 

Let me just post some code from one of my projects since it'll probably convey meaning better than words.  AddressbookContact and PhoneNumber are just a couple models of mine, don't worry about those.

 

private void addContact(Contact contact) { int[] fieldIds = contact.getFields(); for (int index = 0; index < fieldIds.length; index++) { int id = fieldIds[index]; if (id == Contact.NAME) { loadNameFromContact(addressbookContact, contact, id); } else if (id == Contact.TEL) { loadPhoneFromContact(addressbookContact, contact, id); } } }

 

Here's an example of loading a STRING (phone):

 

 

private void loadPhoneFromContact(AddressBookContact abContact, Contact contact, int id) { Vector phoneNumbers = abContact.getPhoneNumbers(); for (int i = 0; i < contact.countValues(id); i++) { PhoneNumber newPhoneNumber = new PhoneNumber(); newPhoneNumber.number = contact.getString(id, i); int attrs = contact.getAttributes(id, i); int[] supportedAttrs = contact.getPIMList().getSupportedAttributes(id); for (int j = 0; j < supportedAttrs.length; j++) { if ((attrs & supportedAttrs[j]) == supportedAttrs[j]) { String attrName = contact.getPIMList().getAttributeLabel(supportedAttrs[j]); newPhoneNumber.attr = attrName; } } phoneNumbers.addElement(newPhoneNumber); } }

 

And here's an example of loading a STRING_ARRAY (name):

 

private void loadNameFromContact(AddressBookContact abContact, Contact contact, int id) { for (int i = 0; i < contact.countValues(id); i++) { String[] values = contact.getStringArray(id, i); abContact.setFirstName(values[Contact.NAME_GIVEN]); abContact.setLastName(values[Contact.NAME_FAMILY]); } }

 

Please use plain text.
BlackBerry Development Advisor
MSohm
Posts: 14,649
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook

Re: IllegalArgumentException from PIMItem.getString(int, int)

We also have a sample on this in our developer knowledge center.  It can be found here:

 

How To - Access Address Book contacts
Article Number: DB-00147

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800363/How_To_...

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Developer
PaoloLim
Posts: 21
Registered: ‎07-16-2008
My Device: Not Specified

Re: IllegalArgumentException from PIMItem.getString(int, int)

Thanks for all the help guys, I was able to extract a bunch of information from that object.  One thing to note however, on both 4.3 and 4.1, the contact object did not store certain pieces of information such as work2 and home2 and I wasn't able to extract them.  It wasn't until 4.5 that I finally saw values for it.  Was there a reason for this?
Please use plain text.
BlackBerry Development Advisor
MSohm
Posts: 14,649
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook

Re: IllegalArgumentException from PIMItem.getString(int, int)

These fields are not supported until BlackBerry handheld software version 4.5.0.  So you are seeing the expected behaviour.
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.