VCard Problem

The following code generates a Null Pointer exception which is thrown from the VCardReader. I think I have created and filled the VCard up properly, but I can’'t be sure as the only way to check that would be using the code below.

My process is as follows:

  1. User fills out form with both account and personal settings (see attached file).

  2. When the user presses ‘‘Create Account’’ I try and create the account using the supplied account settings.

  3. If that is succesful, I log in using the settings given to create the account (connection.login(form.getUserName(), form.getPassword()).

  4. At this point, I fill up a VCard and ‘‘save’’ it (vcard.save(connection)).

I don’‘t use all the VCard fields…doesn’'t seem like that should be required. Does the above logic make sense?

Code:[/b]

VCard vcard = new VCard();

try {

vcard.load(connection);

// Set fields in form

} catch (XMPPException e) {

e.printStackTrace();

}

/code

Error:[/b]

java.lang.NullPointerException

at org.jivesoftware.smackx.provider.VCardProvider$VCardReader.appendText(VCardProv ider.java:198)

at org.jivesoftware.smackx.provider.VCardProvider$VCardReader.getTextContent(VCard Provider.java:193)

at org.jivesoftware.smackx.provider.VCardProvider$VCardReader.setupPhones(VCardPro vider.java:133)

at org.jivesoftware.smackx.provider.VCardProvider$VCardReader.initializeFields(VCa rdProvider.java:109)

at org.jivesoftware.smackx.provider.VCardProvider.parseIQ(VCardProvider.java:80)

at org.jivesoftware.smack.PacketReader.parseIQ(PacketReader.java:484)

at org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:272)

at org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:42)

at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:62)

/code

-Ken

Hey Ken,

Could you paste the XML that your client is sending and receiving from the server? I guess that the client stored a telephone in an incorrect format or something. So it fails when trying to load it and parse it.

Regards,

– Gato

Thanks for the reply Gato.

I don’'t see any of the VCard data in these packets…not sure why.

Sent:[/b]

/code

Received:[/b]

/code

To get to this point, I’'ve:

  1. Logged in

  2. Requested my VCard with the following code:

VCard vcard = new VCard();

try {

vcard.load(connection);

// this.setFirst(vcard.getFirstName());

// this.setLast(vcard.getFirstName());

// this.setEmail(vcard.getEmailHome());

// this.setCity(vcard.getAddressFieldHome(“LOCALITY”));

// this.setState(vcard.getAddressFieldHome(“REGION”));

// this.setZip(vcard.getAddressFieldHome(“PCODE”));

// this.setPhone(vcard.getPhoneHome(“VOICE”));

// this.setURL(vcard.getField(“URL”));

} catch (XMPPException e) {

e.printStackTrace();

}

/code

The NullPointerException is thrown as a result of that vcard.load(connection). I would think that if I was allowed to save the last vcard session (with vcard.save(connection)) then I would be able to safely read from the same vcard.

-Ken

Hey Ken,

In the last received IQ I see that you are getting this vCard: [/code].

As I was expecting the TEL element is not containing a NUMBER element. I’'m going to fix the parsing logic to consider this possible scenario but could you tell me how to generate a vCard that contains a TEL element with no NUMBER element inside?

Thanks,

– Gato

Hey Gato,

Here is the code that I use to write out the VCard:

VCard vcard = new VCard();

vcard.setFirstName(this.getFirst());

vcard.setLastName(this.getLast());

vcard.setEmailHome(this.getEmail());

vcard.setAddressFieldHome(“LOCALITY”, this.getCity());

vcard.setAddressFieldHome(“REGION”, this.getState());

vcard.setAddressFieldHome(“PCODE”, this.getZip());

vcard.setPhoneHome(“VOICE”, this.getPhone());

vcard.setField(“URL”, this.getURL());

vcard.save(connection);

/code

The method this.getPhone()[/code] returns ########## (thats 10 numbers, a U.S. standard phone number).

Thanks Gato,

-Ken

Hey Gato…problem solved!

There were actually two problems:

  1. I was inadvertently setting the phone number to null in the VCard.homePhone hash map.

  2. Once I fixed that, I realized the code that was setting the phone number was setting it to the string representation of an object…here’'s the faulty code:

up.setPhone(this.areaCode.getText() + this.phonePrefix.getText() + this.phonePostfix);

/code

Notice how I forgot the .getText() call on this.phonePostfix()!

The first problem was the real culprit. It seems like there should be some sort of check against a null value in any hash map, which I think you said you were adding.

Thank for your help Gato!

-Ken