AnsweredAssumed Answered

How to create a custom IQ packet

Question asked by calvin chan on Aug 17, 2015
Latest reply on Aug 17, 2015 by calvin chan

I try to create a custom IQ packet to register push token on Android.

Here is my custom IQ class

class MyCustomIQ extends IQ {
    String token;

    protected MyCustomIQ(String token) {
        super("query", "urn:xmpp:registernoti");
        this.token = token;

    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
        xml.element("token", token);
        xml.element("devicetpye", "android");

        return xml;


And here how I create IQ


MyCustomIQ iq = new MyCustomIQ(pushToken);


After I created the IQ packet, I tried to use `toString()` to show the XML in logcat, but it turn to error below. An I am using Smack API version:4.1.3 (4.1.3 2015-07-15)

java.lang.NullPointerException: Attempt to invoke interface method 'int java.lang.CharSequence.length()' on a null object reference
        at org.jivesoftware.smack.util.LazyStringBuilder.length(
        at org.jivesoftware.smack.util.LazyStringBuilder.toString(
        at org.jivesoftware.smack.util.XmlStringBuilder.toString(
        at org.jivesoftware.smack.packet.Stanza.toString(
        at org.jivesoftware.smack.AbstractXMPPConnection.callConnectionAuthenticatedListener(
        at org.jivesoftware.smack.AbstractXMPPConnection.afterSuccessfulLogin(
        at org.jivesoftware.smack.tcp.XMPPTCPConnection.afterSuccessfulLogin(
        at org.jivesoftware.smack.tcp.XMPPTCPConnection.loginNonAnonymously(
        at org.jivesoftware.smack.AbstractXMPPConnection.login(
        at org.jivesoftware.smack.AbstractXMPPConnection.login(