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
Posts: 138
Registered: ‎01-09-2011
My Device: Blackberry Tablet OS

Invalid Argument Exception?

I am getting an invalid argument exception on the following code. The code is meant to add a contact to the user's blackberry. Why am I getting this exception?

 

 

try {
ContactList cl = (ContactList)PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE);
Contact c = cl.createContact();
c.addString(Contact.NAME_GIVEN, PIMItem.ATTR_NONE, "The Red Herring");
c.addString(Contact.EMAIL, PIMItem.ATTR_NONE, "red@herring.com");
c.addString(Contact.NICKNAME, PIMItem.ATTR_NONE, "RH");
c.addString(Contact.ADDR_STREET, PIMItem.ATTR_NONE, "Right Behind You");
c.commit();
cl.close();
} catch (PIMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

 

 

From,
Noah NU
Developer
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: Invalid Argument Exception?

I would suggest adding a breakpoint above the addString()s and see which one is doing it (if they are, that is). According to the API you'll get an IllegalArgumentException if you are adding the wrong type to a field. Another thing to watch for is a field that can have mutliple entries...fought with that myself before.

Developer
Posts: 138
Registered: ‎01-09-2011
My Device: Blackberry Tablet OS

Re: Invalid Argument Exception?

I am getting the exception for each addString() and I even followed the guidelines in the documentation for which data types are acceptable for each field, string, string array, int, etc..

From,
Noah NU
Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Invalid Argument Exception?

NAME is a string array.

 

See this example in the docs:

 

 ContactList contactList = (ContactList)PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.WRITE_ONLY);
if (contactList.isSupportedField(Contact.NAME)) {
     Contact contact = contactList.createContact();
     String[] name = new String[ contactList.stringArraySize( Contact.NAME ) ];
     name[Contact.NAME_GIVEN] = "Kate";
     name[Contact.NAME_FAMILY] = "Turner";
     contact.addStringArray(Contact.NAME, Contact.ATTR_NONE, new String[] {"Kate Turner"});
     contact.commit();
     return true;
}  else {
     return false;
}

Developer
Posts: 138
Registered: ‎01-09-2011
My Device: Blackberry Tablet OS

Re: Invalid Argument Exception?

[ Edited ]

Yes I am aware of that. In my code above I used NAME_GIVEN not name. Also the error occurs on EVERY addString not just the name one. What could be the problem?

 

EDIT: I just looked over your example again and I am a bit confused. You declare and set a String array yet you never use it.

 

Here you set it:

 

 

String[] name = new String[ contactList.stringArraySize( Contact.NAME ) ];
name[Contact.NAME_GIVEN] = "Kate";
name[Contact.NAME_FAMILY] = "Turner";

 

 

Yet here, the place were you add the array, you dont mention your string array name anywhere, you create a new array on the spot.

 

 

contact.addStringArray(Contact.NAME, Contact.ATTR_NONE, new String[] {"Kate Turner"});

 

 

From,
Noah NU
Developer
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: Invalid Argument Exception?

My guess is he probably wrote this to help you out but didn't actually try to compile it. His string array that he made but didn't use is what he most likely meant to pass as an argument. Please remember that we are here to guide you in the right direction, not do your work for you.
Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Invalid Argument Exception?

Sorry, I copied that from the javadoc example.

 

However, if you look closely you will find that the name argument that is passed is in fact an array.

 

Highlighted
Developer
Posts: 138
Registered: ‎01-09-2011
My Device: Blackberry Tablet OS

Re: Invalid Argument Exception?

[ Edited ]

Yes I am aware of that however even when I removed the Name fields I still received the error. I tried using addString on the Address field, the Email field and the Name fields - all together as well as seperately.

 

@

From,
Noah NU