3

I have a class like this:

MainActivity.java

public class MainActivity extends FragmentActivity {
     /* I removed other methods */

     public Map<String, Conversation> conversationsMap = new LinkedHashMap<>();
}

And Connection.java

public class Connection {
/* I removed other methods */

     public class TestMessageListener implements MessageListener {  
           MainActivity mainClass;

           public void someMethod() {
                mainClass.conversationsMap.get("test"); /// This line is returning nullpointer exception
           }
     }
}

Why I can't access conversationsMap from another class ?

5 Answers 5

4

MainActivity mainClass; is a field of nested class TestMessageListener so if you will not initialize it it will be initialized with default value which for references is null which means that

mainClass.conversationsMap.get("test");

will try to invoke conversationsMap.get("test") on null, but since null doesn't have any fields or methods you are getting NullPointerException.

Generally to solve this kind of problem you either initialize mainClass yourself with new instance

MainActivity mainClass = new MainActivity();

but probably better option is to let user or other process pass already created instance to TestMessageListener class. To do this you can create setter,

public void setMainActivity(MainActivity mainClass){
    this.mainClass = mainClass;
}

or accept MainActivity in TestMessageListener constructor

public TestMessageListener(MainActivity mainClass){
    this.mainClass = mainClass;
}

I am not Android developer so they can be even better ways, like maybe getting this instance from some kind registered Activity containers but I can't help you in this case.

Sign up to request clarification or add additional context in comments.

2 Comments

It fixed but this line is still giving nullpointer : ((ConversationsAdapter) mainClass.conversationsAdapter).updateConversations(mainClass.conversationsList); do you know why ? I have a conversationsAdapter class in main class (it is public)
@Okan Sorry, was AFK. Anyway something tells me that this is same problem as in TestMessageListener (uninitialized field) but I can't be sure if I will not see code which will let me reproduce your problem.
3

You could pass the MainActivity context to the Connection constructor and then in the TestMessageListener just do something like:

MainActivity mainClass = (MainActivity) mContext;

Comments

2

You do not have any instance of MainActivity.

Solution is :

MainActivity mainClass = new MainActivity();

Comments

2

You haven't intialized mainClass, that's why you are getting null pointer . You can either create new Main activity(but remember that your map is not static , so any modifications that you made might be lost depending where you made them) like

MainActivity mainClass = new MainActivity(); 

Or Create a constructor in Connection class which accepts context and the pass Main Activity to it and later use it inside your TestMessageListener . E.g.

private MainAcitivity mainClass;
public Connection(Context context) {
mainClass = (MainAcitivity) context;
}

Comments

1
public class TestMessageListener implements MessageListener {
           MainActivity mainClass = new MainActivity();
     }

You need an instance of mainClass in order to interact with it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.