2

I use aSmack and Openfire database to make a chat application. And now I'm stack because of NullPointerException which is occurred when I try to log in. Can somebody help me to solve this issue? I really don't know what to do. Cause connection, loginUser, password are not null. This information is from my logcat.

org.jivesoftware.smack.tcp.XMPPTCPConnection@42c4fca0 connection

AJ1 loginUser

password54 password

My code:

public void connect() {
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                XMPPTCPConnectionConfiguration.Builder ConnectionConfiguration = XMPPTCPConnectionConfiguration.builder();
                ConnectionConfiguration.setServiceName("lucky");
                ConnectionConfiguration.setHost("192.168.100.5");
                ConnectionConfiguration.setPort(5222);
                ConnectionConfiguration.setSecurityMode(org.jivesoftware.smack.ConnectionConfiguration.SecurityMode.disabled);
                ConnectionConfiguration.setDebuggerEnabled(true);
                connection = new XMPPTCPConnection(ConnectionConfiguration.build());
                XMPPConnectionListener xmppConnectionListener = new XMPPConnectionListener();
                connection.addConnectionListener(xmppConnectionListener);
                try {

                    connection.connect();    //Error here

                } catch (SmackException.ConnectionException e) {
                  //  e.printStackTrace();
                    Log.e("Point_1","Exception " + e);
                } catch (SmackException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (XMPPException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
    }
public class XMPPConnectionListener implements ConnectionListener {
        @Override
        public void connected(XMPPConnection connection) {
            if(!connection.isAuthenticated()){

                login();    //Error here

                Log.e("Point_login", "XMPPListener, login()");
            }
            connected = true;
            Log.e("Point_login", "XMPPListener, connected()");
        }

        @Override
        public void authenticated(XMPPConnection connection, boolean resumed) {
            loggedin = true;
            Log.e("Point_login", "XMPPListener, authenticated");
        }

        @Override
        public void connectionClosedOnError(Exception e) {
            Log.e("Point_login", "XMPPListener, connectinClosedOnError " + e);
            loggedin = false;
            connected = false;
            isChatCreated = false;
        }

        @Override
        public void connectionClosed() {
            loggedin = false;
            connected = false;
            isChatCreated = false;
            Log.e("Point_login", "XMPPListener, connectionClosed");
        }

        @Override
        public void reconnectingIn(int seconds) {
            loggedin = false;
            Log.e("Point_login", "XMPPListener, reconnectingIn");
        }

        @Override
        public void reconnectionFailed(Exception e) {
            loggedin = false;
            connected = false;
            isChatCreated = false;
            Log.e("Point_login", "XMPPListener, reconnectionFailed");
        }

        @Override
        public void reconnectionSuccessful() {
            loggedin = false;
            connected = true;
            isChatCreated = false;
            Log.e("Point_login", "XMPPListener, reconnectionSuccessful");
        }


    }
public void login(){
        try{
            Log.e("Point_login", String.valueOf(connection) + " connection");
            Log.e("Point_login", String.valueOf(loginUser) + " loginUser");
            Log.e("Point_login", String.valueOf(password) + " password");

            connection.login(loginUser,password);    //Error here

        }catch(SmackException.NotConnectedException e){
            Log.e("Login_1","Exception");
            for(int i=0;i<=5;i++){
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(1000);
                        }catch (InterruptedException e){
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        }catch(XMPPException | SmackException | IOException e){
            e.printStackTrace();
            Log.e("Login_1","2nd Exception");
        }
    }

My logcat:

E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-2634
    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:427)
            at com.lets.dothis.myapplication.XMPP.login(XMPP.java:180)
            at com.lets.dothis.myapplication.XMPP$XMPPConnectionListener.connected(XMPP.java:206)
            at org.jivesoftware.smack.AbstractXMPPConnection.callConnectionConnectedListener(AbstractXMPPConnection.java:1152)
            at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:841)
            at org.jivesoftware.smack.AbstractXMPPConnection.connect(AbstractXMPPConnection.java:360)
            at com.lets.dothis.myapplication.XMPP$1.run(XMPP.java:150)
            at java.lang.Thread.run(Thread.java:856)

Thank you.

Steve
  • 81
  • 2
  • 10

1 Answers1

0

In login() method pass "XMPPConnection" object which is "connection" , like login(connection)

then, IN.... connection.login(loginUser,password); replace that line with

if(!connection.isAuthenticated())  
            login(connection, loginUser, passwordUser);  
else connection.login(loginUser, passwordUser); 

I hope it will work. Please don't make conflict with two objects with same name XMPPConnection & XMPPTCPConnection , for simplicity , just rename one.

referance

http://xmpp-tutorials.blogspot.com.br/

please have a try & let me know if there is still any exception. thanks

  • I'm sorry but this doesn't work for me cause author used aSmack 4.0.0 while I'm using aSmack 4.1.0 – Steve Jul 26 '16 at 10:34