获取 XMPPConnection 的 Nullpointer 异常
Posted
技术标签:
【中文标题】获取 XMPPConnection 的 Nullpointer 异常【英文标题】:Getting Nullpointer exception for XMPPConnection 【发布时间】:2018-07-17 18:31:17 【问题描述】:我在 localhost 上运行 openfire 服务器, Smack 版本 4.2.4.GetAllContacts() 方法中出现异常。 下面是我的类 XmppConnection 的代码,它扩展了类 org.jivesoftware.smack.ConnectionListener。
public class XmppConnection implements ConnectionListener
private XMPPTCPConnection mConnection;
public List<Contact> getAllContacts() throws SmackException.NotLoggedInException, InterruptedException, SmackException.NotConnectedException
Log.d(TAG,"getAllContats called()");
List<Contact> contacts = new ArrayList<>();
if(XmppConnectService.sConnectionState == ConnectionState.AUTHENTICATED)
Roster roster = Roster.getInstanceFor(mConnection); //exception here
Collection<RosterEntry> entries = roster.getEntries();
Presence presence;
for (RosterEntry entry : entries)
Contact c = new Contact(entry.getJid().toString());
presence = roster.getPresence(entry.getJid());
contacts.add(c);
return contacts;
public XmppConnection( Context context)
mApplicationContext = context.getApplicationContext();
String jid = PreferenceManager.getDefaultSharedPreferences(mApplicationContext)
.getString("xmpp_jid",null);
mPassword = PreferenceManager.getDefaultSharedPreferences(mApplicationContext)
.getString("xmpp_password",null);
if( jid != null)
mUsername = jid.split("@")[0];
mServiceName = jid.split("@")[1];
else
mUsername ="";
mServiceName="";
public void connect() throws IOException,XMPPException,SmackException
Log.d(TAG, "Connecting to server " + mServiceName);
InetAddress addr = InetAddress.getByName(OPENFIRE_HOST);
HostnameVerifier verifier = new HostnameVerifier()
@Override
public boolean verify(String hostname, SSLSession session)
return true;
;
DomainBareJid serviceName = JidCreate.domainBareFrom(OPENFIRE_HOST);
conf = XMPPTCPConnectionConfiguration.builder();
conf.setXmppDomain(serviceName
.setHostAddress(addr)
.setUsernameAndPassword(mUsername,mPassword)
.setPort(5222)
.setHostnameVerifier(verifier)
.setSecurityMode(ConnectionConfiguration.SecurityMode.required)
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.setCompressionEnabled(true);
mConnection = new XMPPTCPConnection(conf.build());
setupUiThreadBroadCastMessageReceiver();
mConnection.addConnectionListener(this);
try
mConnection.connect();
mConnection.login(mUsername,mPassword);
catch (InterruptedException e)
e.printStackTrace();
ReconnectionManager reconnectionManager = ReconnectionManager.getInstanceFor(mConnection);
reconnectionManager.setEnabledPerDefault(true);
reconnectionManager.enableAutomaticReconnection();
@Override
public void connected(XMPPConnection connection)
XmppConnectService.sConnectionState = ConnectionState.CONNECTED;
Log.w(TAG,"Connected Successfully; id:" + XmppConnectService.sConnectionState);
@Override
public void authenticated(XMPPConnection connection, boolean resumed)
XmppConnectService.sConnectionState = ConnectionState.AUTHENTICATED;
Log.w(TAG,"Authenticated Successfully");
showContactListActivityWhenAuthenticated();
以上代码在 logcat 中显示此错误:java.lang.NullPointerException: XMPPConnection must not be null
我从另一个类中调用 getAllContacts()
XmppConnection xmpp = new XmppConnection(this);
contacts = xmpp.getAllContacts();
getAllContacts() 方法从另一个活动调用,并在 authenticated() 方法之后调用。 因为它是在 authenticated() 方法之后调用的,所以 mConnection 应该已经初始化了。
【问题讨论】:
【参考方案1】:正如您的代码所示,您在方法 connect() 中初始化 mConnection 变量。 并且您的方法 authenticated() 不会对方法 connect() 进行任何调用。 您能否确保在调用方法 authenticated() 之前或在方法内调用 connect() 方法
【讨论】:
感谢您的回复。我确信在 connect() 方法之后调用了 authenticated() 方法。 mConnection.connect() 确保先调用 connected(),然后再调用 authenticated()。【参考方案2】:为 XmppConnection 类创建一个新对象会将其变量初始化为 null。 将 XMPPTCPConnection 声明为 static 有效:
private static XMPPTCPConnection mConnection;
【讨论】:
以上是关于获取 XMPPConnection 的 Nullpointer 异常的主要内容,如果未能解决你的问题,请参考以下文章
aSmack 错误:XMPPConnection 是抽象的;无法实例化
ClassNotFoundException 打击 XMPPConnection
我可以从并发线程调用 XMPPConnection.sendPacket 吗?
smack 中 XMPPConnection 的登录方法发生异常
aSmack连接server异常smack.SmackException$ ConnectionException thrown by XMPPConnection.connect();