无法登录,NullPointerException

Posted

技术标签:

【中文标题】无法登录,NullPointerException【英文标题】:Cannot login , NullPointerException 【发布时间】:2016-07-26 06:07:08 【问题描述】:

我使用 aSmack 和 Openfire 数据库来制作聊天应用程序。现在我因为尝试登录时发生 NullPointerException 而被堆栈。有人可以帮我解决这个问题吗?我真的不知道该怎么办。 原因连接、登录用户、密码不为空。此信息来自我的 logcat。

org.jivesoftware.smack.tcp.XMPPTCPConnection@42c4fca0 连接

AJ1 登录用户

password54 密码

我的代码:

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");
        
    

我的日志猫:

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)

谢谢。

【问题讨论】:

从哪里调用 connect() ?? @sjy 来自 Activity 中的 onCreate 方法 您是如何将 Smack 添加到您的项目中的?? @sjy 将 jar 文件放在 libs 文件夹中。并将 xmpp 依赖添加到 buile.gradle。 What is a NullPointerException, and how do I fix it?的可能重复 【参考方案1】:

在 login() 方法中传递 "XMPPConnection" 对象,即 "connection" ,如 login(connection)

然后,IN.... connection.login(loginUser,password); 用

替换该行
if(!connection.isAuthenticated())  
            login(connection, loginUser, passwordUser);  
else connection.login(loginUser, passwordUser); 

我希望它会起作用。 请不要与同名的两个对象发生冲突 XMPPConnection & XMPPTCPConnection ,为简单起见,重命名一个即可。

参考

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

请试一试,如果仍有异常,请告诉我。谢谢

【讨论】:

很抱歉,这对我不起作用,因为作者在使用 aSmack 4.1.0 时使用了 aSmack 4.0.0

以上是关于无法登录,NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章

使用 Facebook SDK 和 ParseFacebookUtils 登录时出现 NullPointerException

Facebook 登录 AsyncTask #4 NullPointerException 启动时崩溃

无法解决 Java Swing 应用程序中的 NullPointerException

严重:处理请求期间发生异常:null java.lang.NullPointerException

无法在 Android 上使用 asmack 登录 Facebook

FireBase 身份验证登录/注册异常