正在获取XMPPConnection的Nullpointer异常



我正在本地主机上运行openfire服务器,Smack版本4.2.4.在getAllContacts((方法中获取异常。下面是我的类XmppConnection的代码,它扩展了类org.livesoftware.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应该已经初始化。

如代码所示,初始化方法connect((中的mConnection变量。并且您的方法authenticated((不会对方法connect((进行任何调用。你能确保方法connect((在调用方法authenticated((之前或在方法内被调用吗

为XmppConnection类创建一个新对象会用null初始化其变量。将XMPPTCPConnection声明为静态有效:

private static XMPPTCPConnection mConnection;

最新更新