Search code examples
javanullpointerexceptionxmppsmacksasl

Smack 4.2.0 NullPointerException when logging in


I'm still new in using Smack API. I got this exception while trying to log in.

Here's my code :

XMPPTCPConnectionConfiguration configuration = XMPPTCPConnectionConfiguration.builder()
                .setUsernameAndPassword(username, password)
                .setXmppDomain(domain)
                .setHostAddress(InetAddress.getByName(host))
                .setPort(port)
                .build();
XMPPTCPConnection connection = new XMPPTCPConnection(configuration);
connection.connect();
connection.login();

Here's my log :

Exception in thread "main" java.lang.NullPointerException
at java.util.Collections$UnmodifiableCollection.<init>(Collections.java:1026)
at java.util.Collections$UnmodifiableSet.<init>(Collections.java:1128)
at java.util.Collections.unmodifiableSet(Collections.java:1118)
at org.jivesoftware.smack.ConnectionConfiguration.getEnabledSaslMechanisms(ConnectionConfiguration.java:476)
at org.jivesoftware.smack.SASLAuthentication.selectMechanism(SASLAuthentication.java:359)
at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:191)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.loginInternal(XMPPTCPConnection.java:385)
at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:491)
at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:448)
at Main.main(Main.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Solution

  • Case closed. At first, I only used smack core and smack tcp as libraries. It turns out that you need to use smack sasl as a library in my case. Download smack here and copy smack sasl jar into libs folder as a library.