Problem Using Source

Hi,

I am trying to use the source code of Smack, but getting exceptions while displaying the Roster entries. Any ideas why I’m getting this exception.

I’m trying to run the smack using the orginial source code.

==== Your Roster Have ======

java.lang.IllegalArgumentException: org.jivesoftware.smack.packet.RosterPacket$ItemType is not an enum type

at java.lang.Class.enumConstantDirectory(Class.java:2895)

at java.lang.Enum.valueOf(Enum.java:187)

at org.jivesoftware.smack.packet.RosterPacket$ItemType.valueOf(RosterPacket.java:2 68)

at org.jivesoftware.smack.PacketReader.parseRoster(PacketReader.java:697)

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

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

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

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

Regards,

Suhaib.

sasuhaib wrote:

java.lang.IllegalArgumentException: org.jivesoftware.smack.packet.RosterPacket$ItemType is not an enum type

You are trying to use it (RosterPacket.ItemType) as an Enum, which it isn’t (in your version of smack? in my documentation of smack 3.0.4, it’s supposedly an Enum).

Some source code (using the code tags code here) that is causing this error would help understand the problem better.

//======== PacketReader.java (line: 679-698) ======
    private RosterPacket parseRoster(XmlPullParser parser) throws Exception {
        RosterPacket roster = new RosterPacket();
        boolean done = false;
        RosterPacket.Item item = null;
        while (!done) {
            int eventType = parser.next();
            if (eventType == XmlPullParser.START_TAG) {
                if (parser.getName().equals("item")) {
                    String jid = parser.getAttributeValue("", "jid");
                    String name = parser.getAttributeValue("", "name");
                    // Create packet.
                    item = new RosterPacket.Item(jid, name);
                    // Set status.
                    String ask = parser.getAttributeValue("", "ask");
                    RosterPacket.ItemStatus status = RosterPacket.ItemStatus.fromString(ask);
                    item.setItemStatus(status);
                    // Set type.
                    String subscription = parser.getAttributeValue("", "subscription");
                    RosterPacket.ItemType type = RosterPacket.ItemType.valueOf(subscription);
                    item.setItemType(type); //==== RosterPacket.java (line:268-294) ========     public static enum ItemType {         /**
         * The user and subscriber have no interest in each other's presence.
         */
        none,         /**
         * The user is interested in receiving presence updates from the subscriber.
         */
        to,         /**
         * The subscriber is interested in receiving presence updates from the user.
         */
        from,         /**
         * The user and subscriber have a mutual interest in each other's presence.
         */
        both,         /**
         * The user wishes to stop receiving presence updates from the subscriber.
         */
        remove
    }

sasuhaib wrote:

> > //======== PacketReader.java (line: 679-698) ======
> >     // Set type.
> >     String subscription = parser.getAttributeValue("", "subscription");
> >     RosterPacket.ItemType type = RosterPacket.ItemType.valueOf(subscription);
> >

Okay. Your variable subscription could contain a String that isn’t a representative of a RosterPacket.ItemType.

From the docs:

valueOf

public static RosterPacket.ItemType valueOf(String name)

Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)

Parameters:

name - the name of the enum constant to be returned.

Returns:

the enum constant with the specified name

Throws:

IllegalArgumentException - if this enum type has no constant with the specified name

To get around this uncertainty, surround the statement with a try-catch block, so you can take appropriate action (assign a default value, or give a better error message)…