I am running openfire server on localhost, Smack version 4.2.4.Getting exception in getAllContacts() method. Below is the code for my class XmppConnection which extends 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();
}
}
}
Above code is showing this error in logcat :java.lang.NullPointerException: XMPPConnection must not be null
I'm calling getAllContacts() from another class as
XmppConnection xmpp = new XmppConnection(this);
contacts = xmpp.getAllContacts();
getAllContacts() method is called from another activity and is called after authenticated() method. Since,it is called after authenticated() method, mConnection should have initialized.
Making a new object for XmppConnection class initializes its variables with null. Declaring XMPPTCPConnection as static works:
private static XMPPTCPConnection mConnection;