4 Replies - 329 Views - Last Post: 12 June 2020 - 06:54 AM Rate Topic: -----

#1 TheStudent555   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 06-February 19

Interface object is null in an asynchronous process

Posted 11 June 2020 - 11:23 PM

public interface Listener {

    void on_data_received(MutableLiveData<ArrayList<Object>> object_data);
    void on_error();

}




ObjectRepository
   private final MutableLiveData<ArrayList<Object>> object_data = new MutableLiveData<>();
    private ArrayList<Object> objects = new ArrayList<>();
   private Listener listen;

    public void get_object_data(Listener listen){
       
      this.listen = listen;   // <-- This is called. Always not null 

    }

// Network Call is called in sync adapter class, in a function called onPerformSync. Function is called every 2 min 
  private void network_call(){ 

        Network network = new Network();
        network.Execute(new NetworkCallback() {
            @Override
            public void network_response(String json_response) {

          
                JSONArray server_response = null;
                try{

                    server_response = new JSONArray(json_response);

                }catch(JSONException json_error){
                    json_error.printStackTrace();
                }

          

                objects.clear();

                for(int i = 0; i < server_response.length(); i++){

                    try {

                        String var_a = server_response.getJSONObject(i).getString("variable_a");
                        String var_b = server_response.getJSONObject(i).getString("variable_b");

                        Object obj = new Object();
                        obj.var_a = var_a;
                        obj.var_b = var_b;

                        objects.add(obj);

                    }catch(JSONException json_error){
                        json_error.printStackTrace();
                        return;
                    }


                }

            
                object_data.setValue(objects);
                if(listen != null) {
                                                                           
                    listen.on_data_received(object_data);   // <-- Never called. listen is always null
                }
             
            }

            @Override
            public void network_error(VolleyError error) {
           
                error.printStackTrace();
            }
        });

    }





FragmentViewModel.class
 private MutableLiveData<ArrayList<Object>> object = new MutableLiveData<ArrayList<Object>>;
 public void initialize(Application application){

        if(repository == null) {
            object_repository = ObjectRepository.getInstance();
        }


        object_repository = ObjectRepository.getInstance();
        repository.get_object_data(new Listener() {
            @Override
            public void on_data_received(MutableLiveData<ArrayList<Object>> object_data) {

         
                object = object_data;
            }

            @Override
            public void on_error() {

                Log.d(TAG, "An Error occurred");
            }
        });
    }

    public MutableLiveData<ArrayList<Object>> getObject(){
        return object;
    }




Having trouble with an interface object being null in a asynchronous process. The asynchronous process, in this case, is a remote server call. When the call is returned onResponse is called, then the code in the function executes. However, the listen object is null, despite the fact that it is set in the get_object_data(). The network_call() is called in a function called onPerformSync, which belongs to a separate class.

My goal is to pass the MutableLiveData object to the functions in the listen object. But it is currently always null. What could be the problem?

Is This A Good Question/Topic? 0
  • +

Replies To: Interface object is null in an asynchronous process

#2 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2410
  • View blog
  • Posts: 5,050
  • Joined: 11-December 07

Re: Interface object is null in an asynchronous process

Posted 12 June 2020 - 01:14 AM

Is it possible that multiple instances of the repository exist, so the one you are setting the listener in is different from the one receiving the event?
Was This Post Helpful? 0
  • +
  • -

#3 nicephotog   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 34
  • Joined: 06-June 20

Re: Interface object is null in an asynchronous process

Posted 12 June 2020 - 04:56 AM

In the object repository code window there (appears to be asynchronous networking code) you have a method "private void network_call(){" that in turn has a method inside it and has a class then method with a declaration "public", a massive constradictioon at best.
In a public class you can raise the field , method and class modifier security but you cannot lower it in terms of hereditory encapsulation and mixture of levels or it contradicts.

This post has been edited by nicephotog: 12 June 2020 - 04:59 AM

Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3698
  • View blog
  • Posts: 16,952
  • Joined: 20-September 08

Re: Interface object is null in an asynchronous process

Posted 12 June 2020 - 06:24 AM

Where is the log of the error?
Was This Post Helpful? 0
  • +
  • -

#5 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: Interface object is null in an asynchronous process

Posted 12 June 2020 - 06:54 AM

Where is the variable: repository (Used on line 10) declared and where is it given a value?
Is the method that sets the value of listener called before the code tries to use the value in listener?

Note: get_object_data is a confusing name for a method that does not return anything (that is what get methods do) and that does set a value (Listener). A name like setListener would be less confusing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1