Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMPP client using Smack 4.1 giving NullPointerException during login

I am trying to use Smack 4.1.0-rc3 for implementing a java xmpp client which connects to a ejabberd xmpp server. I am using the following code for connecting to the server.

    XMPPTCPConnectionConfiguration connConfig =    XMPPTCPConnectionConfiguration
            .builder()
            .setServiceName("example.com")
            .setHost("192.168.56.101")
            .setPort(5222)
            .setCompressionEnabled(false)
            .setSecurityMode(SecurityMode.disabled)
            .setHostnameVerifier(new HostnameVerifier() {
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }
            })
            .setUsernameAndPassword(user, pass).build();

    connection = new XMPPTCPConnection(connConfig);
    connection.connect();
    connection.login();

While executing the 'connection.login()' i am getting the following NullPointerException.

Exception in thread "main" java.lang.NullPointerException
at org.jivesoftware.smack.util.stringencoder.Base64.encode(Base64.java:64)
at org.jivesoftware.smack.util.stringencoder.Base64.encode(Base64.java:60)
at org.jivesoftware.smack.util.stringencoder.Base64.encodeToString(Base64.java:42)
at org.jivesoftware.smack.sasl.SASLMechanism.authenticate(SASLMechanism.java:199)
at org.jivesoftware.smack.sasl.SASLMechanism.authenticate(SASLMechanism.java:169)
at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:236)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.loginNonAnonymously(XMPPTCPConnection.java:365)
at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:452)
at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:410)
at org.org.oodlezz.unio.jabber.client.XmppClient.connect(XmppClient.java:88)
at org.org.oodlezz.unio.jabber.client.Client.main(Client.java:32)

Am I doing something wrong in the code? Can you please point me towards a proper example for using Smack 4.1.0-rc3?

like image 512
Deepu Avatar asked Mar 14 '15 06:03

Deepu


4 Answers

The other answers on this page have parts of the answer, but trying them I figured out what is really missing is the dependency on smack-java7 library. Adding this dependency causes the initializers to be called, the base64encoder to be set, and so this NullPointerException disappears.

On Android, replace smack-java7 with smack-android.

like image 170
Noam Ben Ari Avatar answered Nov 16 '22 06:11

Noam Ben Ari


Your code is OK, but maybe need the correct dependencies, you can put this on your pom.xml file

<dependencies>
    <dependency>
        <groupId>org.igniterealtime.smack</groupId>
        <artifactId>smack-java7</artifactId>
        <version>4.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.igniterealtime.smack</groupId>
        <artifactId>smack-tcp</artifactId>
        <version>4.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.igniterealtime.smack</groupId>
        <artifactId>smack-im</artifactId>
        <version>4.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.igniterealtime.smack</groupId>
        <artifactId>smack-extensions</artifactId>
        <version>4.1.1</version>
    </dependency>
</dependencies>

This is based on: https://github.com/igniterealtime/Smack/wiki/Smack-4.1-Readme-and-Upgrade-Guide

You can put the version of smack that you need.

About some first steps in maven if you are not familiar with it, this could be useful for you: https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

Hope that this help!

like image 41
jmmorlesin Avatar answered Nov 16 '22 06:11

jmmorlesin


As suggested by Stephen Base64encoder is indeed coming null because its not being set. To initialise that you have to include smack-java7 module in your project.

Before connecting you have to initialize smack using,

new Java7SmackInitializer().initialize();

For Android, I believe you can achieve that using,

new AndroidSmackInitializer().initialize();
like image 42
Abhishek Patidar Avatar answered Nov 16 '22 08:11

Abhishek Patidar


I'm using 4.1.0-rc3. In my case, I didn't meet NPE. The example works.

The following is the part of pom.xml.

pom.xml

<properties>
    <smack.version>4.1.0-rc3</smack.version>
</properties>
...
<dependencies>
    <dependency>
        <groupId>org.igniterealtime.smack</groupId>
        <artifactId>smack-core</artifactId>
        <version>${smack.version}</version>
    </dependency>
    <dependency>
        <groupId>org.igniterealtime.smack</groupId>
        <artifactId>smack-tcp</artifactId>
        <version>${smack.version}</version>
    </dependency>
    <dependency>
        <groupId>org.igniterealtime.smack</groupId>
        <artifactId>smack-java7</artifactId>
        <version>${smack.version}</version>
    </dependency>
...
</dependencies>
...

An sample code is just,

AbstractXMPPConnection conn = new XMPPTCPConnection("xxx", "yyy", "zzz");

conn.connect();
conn.login();
like image 21
cybaek Avatar answered Nov 16 '22 08:11

cybaek