获取 XMPPConnection 的 Nullpointer 异常
Getting Nullpointer exception for XMPPConnection
我是 运行 本地主机上的 openfire 服务器,
Smack 版本 4.2.4.Getting getAllContacts() 方法中出现异常。
下面是我的 class XmppConnection 的代码,它扩展了 class 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
我正在从另一个 class 调用 getAllContacts() 作为
XmppConnection xmpp = new XmppConnection(this);
contacts = xmpp.getAllContacts();
getAllContacts() 方法从另一个 activity 调用,并在 authenticated() 方法之后调用。
因为,它是在 authenticated() 方法之后调用的,mConnection 应该已经初始化。
如您的代码所示,您在方法 connect() 中初始化了 mConnection 变量。
并且您的方法 authenticated() 不会对方法 connect() 进行任何调用。
您能否确保在调用方法 authenticated() 之前或在方法
中调用方法 connect()
为 XmppConnection 创建一个新对象 class 将其变量初始化为 null。
将 XMPPTCPConnection 声明为 static 有效:
private static XMPPTCPConnection mConnection;
我是 运行 本地主机上的 openfire 服务器, Smack 版本 4.2.4.Getting getAllContacts() 方法中出现异常。 下面是我的 class XmppConnection 的代码,它扩展了 class 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
我正在从另一个 class 调用 getAllContacts() 作为
XmppConnection xmpp = new XmppConnection(this);
contacts = xmpp.getAllContacts();
getAllContacts() 方法从另一个 activity 调用,并在 authenticated() 方法之后调用。 因为,它是在 authenticated() 方法之后调用的,mConnection 应该已经初始化。
如您的代码所示,您在方法 connect() 中初始化了 mConnection 变量。 并且您的方法 authenticated() 不会对方法 connect() 进行任何调用。 您能否确保在调用方法 authenticated() 之前或在方法
中调用方法 connect()为 XmppConnection 创建一个新对象 class 将其变量初始化为 null。 将 XMPPTCPConnection 声明为 static 有效:
private static XMPPTCPConnection mConnection;