Suppose A user lost his connection while chatting, but he sent some messages. After a successful reconnection, Chatting App needs to send unsent messages. I used this code for sending message configured in Smack Connection Configuration
private void sendMessage(String body, String toJid) throws XmppStringprepException {
EntityBareJid jid = null;
try {
jid = JidCreate.entityBareFrom("monim@blah.im");
} catch (XmppStringprepException e) {
e.printStackTrace();
}
ChatManager chatManager = ChatManager.getInstanceFor(connection);
Chat chat = chatManager.chatWith(jid);
try {
Message message = new Message(jid, Message.Type.chat);
message.setBody(body);
chat.send(message);
}catch (SmackException.NotConnectedException | InterruptedException e) {
e.printStackTrace();
}
}
and this one is for calling sendMessage() method from Messages activity
private void sendMessage(String message, String receiverJid) {
if (SmackConnectionService.getConnectionState().equals(SmackConnection.ConnectionState.CONNECTED)) {
Intent intent1 = new Intent(SmackConnectionService.SEND_MESSAGE);
intent1.putExtra(SmackConnectionService.BUNDLE_MESSAGE_BODY, message);
intent1.putExtra(SmackConnectionService.BUNDLE_TO, receiverJid);
DefaultMessagesActivity.this.sendBroadcast(intent1);
} else {
saveInDatabases();
Toast.makeText(DefaultMessagesActivity.this, "Offline! Message not sent!", Toast.LENGTH_LONG).show();
}
}
This works fine until I disconnect internet and then reconnect it. While trying to call sendMessage() method to send unsent messages from database after successful reconnection, this error occurs
E/AbstractXMPPConnection: Exception in authenticated listener
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.Context.sendBroadcast(android.content.Intent)' on a null object reference
at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:396)
at com.chatmate.user.chatapp.DefaultMessagesActivity.sendMessage(DefaultMessagesActivity.java:113)
at com.chatmate.user.chatapp.DefaultMessagesActivity.sendMessageAgain(DefaultMessagesActivity.java:146)
at com.chatmate.user.chatapp.SmackConnection.showContactListActivityWhenAuthenticated(SmackConnection.java:272)
at com.chatmate.user.chatapp.SmackConnection.authenticated(SmackConnection.java:229)
at org.jivesoftware.smack.AbstractXMPPConnection.callConnectionAuthenticatedListener(AbstractXMPPConnection.java:1228)
at org.jivesoftware.smack.AbstractXMPPConnection.afterSuccessfulLogin(AbstractXMPPConnection.java:572)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.afterSuccessfulLogin(XMPPTCPConnection.java:377)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.loginInternal(XMPPTCPConnection.java:395)
at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:491)
at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:448)
at org.jivesoftware.smack.ReconnectionManager$2.run(ReconnectionManager.java:254)
at java.lang.Thread.run(Thread.java:818)
where error DefaultMessagesActivity.java:113
indicates DefaultMessagesActivity.this.sendBroadcast(intent1);
this line.