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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Highlighted
Developer
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
Developer
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)); } }

 


 

 

 

Developer
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!
Developer
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?

Developer
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?  Smiley Happy

 

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]); } }

 

BlackBerry Development Advisor
Posts: 15,885
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

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.
Developer
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?
BlackBerry Development Advisor
Posts: 15,885
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

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.