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?
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.
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!
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();
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With