1

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.

gamerrishad
  • 66
  • 1
  • 10
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – nbrooks Feb 03 '18 at 19:01
  • I've tested with if (DefaultMessagesActivity.this.equals(null)) before invoking DefaultMessagesActivity.this.sendBroadcast(intent1). – gamerrishad Feb 03 '18 at 19:30
  • If the object is null then calling `.equals` on it still throws a NullPointerException. Null checks always use the `!=` operator. `if (object != null) { ... }` is the correct syntax. – nbrooks Feb 03 '18 at 19:36
  • Yes, I've checked using `!=` too. And IDE says `Condition 'DefaultMessagesActivity.this != null' is always 'true` – gamerrishad Feb 03 '18 at 21:53

1 Answers1

0

private void sendMessage(String message, String receiverJid) was calling from a class, which was implementing ConnectionListener. The connected() method was called after resuming XMPPTCPConnection and after that, authenticated() was called. The problem is, when I was trying to call sendMessage in DefaultMessagesActivity.class the line DefaultMessagesActivity.this.sendBroadcast(intent1); invoked. But this line called from @overwrite method, which caused NULL pointed Exception in Authenticated Listener. When I was trying to test is DefaultMessagesActivity is null or not, I found it never been a null. But broadcasting in this object is no help for Authenticated Listener.

Solution

If I have to call sendMessage() method to send offline message, then I should write separate code in the file containing @overwrite method connected(). and for broadcasting we use Context.sendBroadcast(intent1);

gamerrishad
  • 66
  • 1
  • 10