powered by Jive Software

Nullpointerexception while parsing the custom iq


#1

I get an exception:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at org.jivesoftware.smack.util.XmlStringBuilder.element(XmlStringBuilder.java:78)
	at univie.ac.at.xmpp.iq.CustomIq.getIQChildElementBuilder(CustomIq.java:24)
	at org.jivesoftware.smack.packet.IQ.getChildElementXML(IQ.java:171)
	at org.jivesoftware.smack.packet.IQ.toXML(IQ.java:140)
	at org.jivesoftware.smack.packet.IQ.toXML(IQ.java:43)
	at org.jivesoftware.smackx.debugger.EnhancedDebugger$20.run(EnhancedDebugger.java:821)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$500(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)

This is my CustomIqProvider class :

public class CustomIqProvider extends IQProvider<CustomIq> {
	@Override
	public CustomIq parse(XmlPullParser parser, int initialDepth) throws Exception {
		String state = null;
		
		outerloop: while(true) {
			int eventType = parser.next();
			switch(eventType) {
				case XmlPullParser.START_TAG:
					String elementName = parser.getName();
					state = parser.nextText();
					break;
				case XmlPullParser.END_TAG:
					if(parser.getDepth() == initialDepth) break outerloop;
					break;
			}
		}
		return new CustomIq(state);
	}
}

Debugger shows that the stanza looked like this before the exception was thrown:
image
And this is how my IQChildElementXmlStringBuilder in CustomIq looks like

@Override
	protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
		xml.rightAngleBracket();
		xml.element("state", state);
		return xml;
	}

#2

The NPE happens at

org.jivesoftware.smack.util.XmlStringBuilder.element(XmlStringBuilder.java:78)

If I had to blindly guess, then I said state of your CustomIQ is null at the time toXml() is invoked.


#3

It was actually the enhanced debugger that caused the error. Thanks for the help !


#4

Cautious my friend. I was likely the enhanced debugger which uncovered the bug. But your code is possibly still not correct.


#5

Hmm, ok so this is where the exception is thrown:

 @Override
    public void onIncomingStreamElement(final TopLevelStreamElement streamElement) {
        final SimpleDateFormat dateFormatter = new SimpleDateFormat("HH:mm:ss:SS");
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                addReadPacketToTable(dateFormatter, streamElement);
            }
        });
    }

It calls invokeLater three times and on the third call it throws an error.

The variables look like this:
image

I noticed that state is null for some reason. But shouldn’t it be equal to ON according to the iq stanza i sent:

<iq type="set" id="light-1" to="admin@laptop-api8s7db/6x8jyn0yfi" from="user@laptop-api8s7db/JMeter">
  <query xmlns="custom:iq:provider">ON</query>
</iq>

Enhanced debugger shows the received iq stanza packet only in “Raw received packets” tab and not in “All packets” tab. I don’t know if that makes any difference, but it’s something i noticed.


#6

Ok code is actually fine. It’s the format of the iq stanza that i was sending that was wrong.
Now there is no exception when i sent iq stanza like this:

<iq type="set" id="light-1" to="admin@laptop-api8s7db/582cbxoizt">
  <query xmlns="custom:iq:provider">
    <state>ON</state>
  </query>
</iq>

Thank you again for the help! Without your guidance i wouldn’t even realise i made this mistake :slight_smile: