0

I'm getting a null pointer exception in some cases. This happens when I reconnect the connection when app goes in foreground because I disconnect the xmpp connection on background to save battery.

The error stacktrace from crash report is:

java.util.Collections$UnmodifiableCollection.<init> (Collections.java:1099)
java.util.Collections$UnmodifiableSet.<init> (Collections.java:1203)
java.util.Collections.unmodifiableSet (Collections.java:1193)
org.jivesoftware.smack.ConnectionConfiguration.getEnabledSaslMechanisms (ConnectionConfiguration.java:476)
org.jivesoftware.smack.SASLAuthentication.selectMechanism (SASLAuthentication.java:359)
org.jivesoftware.smack.SASLAuthentication.authenticate (SASLAuthentication.java:191)
org.jivesoftware.smack.tcp.XMPPTCPConnection.loginInternal (XMPPTCPConnection.java:385)
org.jivesoftware.smack.AbstractXMPPConnection.login (AbstractXMPPConnection.java:491)
org.jivesoftware.smack.AbstractXMPPConnection.login (AbstractXMPPConnection.java:448)
rnxmpp.service.XmppServiceSmackImpl$1.doInBackground (XmppServiceSmackImpl.java:133)
rnxmpp.service.XmppServiceSmackImpl$1.doInBackground (XmppServiceSmackImpl.java:115)
android.os.AsyncTask$2.call (AsyncTask.java:316)
java.util.concurrent.FutureTask.run (FutureTask.java:237)
android.os.AsyncTask$SerialExecutor$1.run (AsyncTask.java:255)
java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1133)
java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:607)
java.lang.Thread.run (Thread.java:776)

Here's the snippet from the android code.

public void connect() {
    if (connection == null) {
        throw new RuntimeException("Cannot connect no connection!");
    }

    new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            try {

                /*
                 * Normally, the reconnection logic would be handled by
                 * the ReconnectionManager, but it seems that this does
                 * not work reasonably well on Android. Therefore, we
                 * provide this naive implementation ourselves.
                 *
                 * See https://stackoverflow.com/a/38178025
                 */
                if (!connection.isConnected()) {
                    connection.connect();
                }
                if (!connection.isAuthenticated()) {
                    connection.login();
                }
            } catch (InterruptedException| XMPPException | SmackException | IOException e) {
                logger.log(Level.SEVERE, "Could not login user", e);
                if (e instanceof SASLErrorException) {
                    XmppServiceSmackImpl.this.xmppServiceListener.onLoginError(((SASLErrorException) e).getSASLFailure().toString());
                } else {
                    XmppServiceSmackImpl.this.xmppServiceListener.onError(e);
                }

            }
            return null;
        }

        @Override
        protected void onPostExecute(Void dummy) {

        }
    }.execute();
}
Jan
  • 3,393
  • 4
  • 21
  • 47

0 Answers0