I am working on an Android project in which I am trying to integrate dependency-injection, because that's the last choice I have for integrating some chat services.
The situation is, I am using framework Cometd for PUSH services. Now, in my platform, there are Conversation
and there are ChatMessages
. In the Conversation class, I am asking the server to give all the friends the user has, so I can subscribe
to them, and then send/receive updates.
The unfortunate problem is, whenever a conversation
is
clicked, it starts new Activity called ChatMessages and shows a list of
messages between the users. But now the Conversation class is gone, I
don't want to instantiate it, so I thought why not inject its dependency
and create 2 methods in each class, which will send/receive messages to
each other and update as well. Well, it didn't fan-out.. :-(
Error log :
java.lang.NullPointerException: Attempt to invoke virtual method 'void mycompany.twentynotes.Activity.ChatMessagesActivity.recieveUpdatedMessage(java.lang.String, java.util.Map)' on a null object reference 12-01 15:57:27.871 27424-27860/mycompany.twentynotes W/System.err: at mycompany.twentynotes.Activity.ConversationActivity.sendMessageToChatActivity(ConversationActivity.java:258) 12-01 15:57:27.871 27424-27860/mycompany.twentynotes W/System.err: at mycompany.twentynotes.Activity.ConversationActivity$ChatListener.onMessage(ConversationActivity.java:319) 12-01 15:57:27.871 27424-27860/mycompany.twentynotes W/System.err: at org.cometd.common.AbstractClientSession$AbstractSessionChannel.notifyOnMessage(AbstractClientSession.java:501) 12-01 15:57:27.871 27424-27860/mycompany.twentynotes W/System.err: at org.cometd.common.AbstractClientSession$AbstractSessionChannel.notifyMessageListeners(AbstractClientSession.java:491)
What I did to get the above error was send a message from browser, which when received by ConversationActivity will be passed to ChatMessages class.
ConversationActivity.java :
public class ConversationActivity extends ApplicationDrawerLoader { public final ChatMessagesActivity chatMessagesActivity; @Inject public ConversationActivity(ChatMessagesActivity chatMessagesActivity){ this.chatMessagesActivity = chatMessagesActivity; } public ConversationActivity(){ chatMessagesActivity = null; } public void sendMessageToChatActivity(String channelName, Map<String,Object> outputData){ Log.d("We are","in send"); this.chatMessagesActivity.recieveUpdatedMessage(channelName, outputData); } // And message arrives in class below : public class ChatListener implements ClientSessionChannel.MessageListener { public void onMessage(ClientSessionChannel channel, Message message) { sendMessageToChatActivity(channel.toString(),message); Log.d("Message is ", message.getJSON()); } } }
ChatMessagesActivity.java :
public class ChatMessagesActivity extends ApplicationDrawerLoader { // I require the ConversationActivity class in here, so I can send new messages which are sent by user to PUSH service. private final ConversationActivity conversationActivity; @Inject public ChatMessagesActivity(ConversationActivity conversationActivity){ this.conversationActivity = conversationActivity; } public ChatMessagesActivity(){ conversationActivity = null; } public void recieveUpdatedMessage(String channelName, Map<String, Object> input){ Log.d("We recieved the","message in CM"); } }
Please note I have added the dagger dependency, but the @Inject
is still from javax.inject. I read that Dagger works/implements it, so I hope that's correct then.
build.gradle :
Now, Is this the correct approach, can you help me with solving the issue, if not a good approach, kindly tell me what should I do..Thanks a lot.. :-)
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile 'com.android.support:appcompat-v7:22.2.1' compile 'com.android.support:appcompat-v7:22.2.1' compile 'com.fasterxml.jackson.core:jackson-core:2.6.0' compile 'com.fasterxml.jackson.core:jackson-databind:2.6.0' compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE' compile 'org.springframework.android:spring-android-core:1.0.1.RELEASE' compile('org.springframework.android:spring-android-auth:1.0.1.RELEASE') { exclude group: 'org.springframework', module: 'commons-logging' exclude group: 'org.springframework', module: 'spring-core' exclude group: 'org.springframework', module: 'spring-web' } compile 'com.fasterxml.jackson.core:jackson-annotations:2.6.0' compile 'com.google.android.gms:play-services:7.5.0' compile 'com.tonicartos:superslim:0.4.13' compile 'commons-codec:commons-codec:1.9' compile 'com.squareup.picasso:picasso:2.5.2' compile 'org.cometd.java:bayeux-api:3.0.6' compile 'org.cometd.java:cometd-java-server:3.0.6' compile 'org.cometd.java:cometd-java-client:3.0.6' compile 'org.cometd.java:cometd-java-annotations:3.0.6' compile 'org.slf4j:slf4j-simple:1.7.12' compile 'com.google.dagger:dagger:2.0.2' compile 'com.google.dagger:dagger-compiler:2.0.2' }