skip to Main Content

i have a method where i load the followers of the specific id from the twitter API, i use it twice in the class, one to load the logged in user's followers and one for other user's followers. The logged in user’s followers worked fine and the code is similar to below, just different id. The code below is for loading followers for other users. I guess it’s that onResponse is not being called in the method and thus otherFollowers is null. Can i know why?

public interface ServiceListeners2 {
//For getting friends :  @GET("1.1/friends/list.json")
@GET("1.1/followers/list.json")
Call<FollowersResponseModel> list(@Query("user_id") long id);
}

StackTrace

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: chuan.twittertwitterlittlestar, PID: 9957
              java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
                  at chuan.twittertwitterlittlestar.FollowerFragment.loadOtherFollowers(FollowerFragment.java:256)
                  at chuan.twittertwitterlittlestar.FollowerFragment.access$100(FollowerFragment.java:50)
                  at chuan.twittertwitterlittlestar.FollowerFragment$3.onItemClick(FollowerFragment.java:175)
                  at android.widget.AdapterView.performItemClick(AdapterView.java:318)
                  at android.widget.AbsListView.performItemClick(AbsListView.java:1165)
                  at android.widget.AbsListView$PerformClick.run(AbsListView.java:3134)
                  at android.widget.AbsListView$3.run(AbsListView.java:4049)
                  at android.os.Handler.handleCallback(Handler.java:789)
                  at android.os.Handler.dispatchMessage(Handler.java:98)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6541)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Application terminated.

In onCreateView,

    mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            if (twitterFollowers.get(position).getId() != 0){
                follower_num = loadOtherFollowers(twitterFollowers.get(position).getId());
                MyCustomAlertDialog(twitterFollowers.get(position).getProfilePictureUrl(), follower_num);
                Toast.makeText(getActivity(), twitterFollowers.get(position).getScreenName(),Toast.LENGTH_LONG).show();
                //sendMsg(twitterFriends.get(position).getId(),twitterFriends.get(position).getScreenName(),"");
            }

        }

    });

2

Answers


  1. Ok here is the thing. You are trying to call a synchronous method to return something that will be created when the Network Call to Twitter API is done. But until it is done and the results are fetched the list is null so the app will always crash. here is what you can do.

    Create a listener interface that will act as the bridge between the data from the API and the usage in your list like this:

    interface MyListener {
      void onFollowersReceived(int listSize);
    }
    

    and after this modify this method like so:

    private void loadOtherFollowers(long id, MyListener listener) {
        Log.e("ID","id is "+id);
        MyTwitterApiClient myTwitterApiClient2 = new MyTwitterApiClient(twitterSession);
        myTwitterApiClient2.getCustomTwitterService2().list(id).enqueue(new retrofit2.Callback<FollowersResponseModel>() {
            @Override
            public void onResponse(Call<FollowersResponseModel> call, Response<FollowersResponseModel> response) {
                Log.e("onResponse",response.toString());
                otherFollowers = fetchResults2(response);
                Log.e("onResponse","otherFollowers:" +otherFollowers.size());
                listener.onFollowersReceived(otherFollowers.size());
            }
    
            @Override
            public void onFailure(Call<FollowersResponseModel> call, Throwable t) {
                Log.e("onFailure",t.toString());
                listener.onFollowersReceived(0);// here you can also put -1 and handle it as error
            }
        });
    }
    

    and then in your list it will be something like this:

    mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
    
                if (twitterFollowers.get(position).getId() != 0){
                    // Change it to whatever item you have
                    Item item = twitterFollowers.get(position);
                    loadOtherFollowers(item.getId(), new MyListener(){
                        @Override
                        public void onFollowersReceived(int listSize) {
                           if(listSize > 0) {
                               MyCustomAlertDialog(item.getProfilePictureUrl(), listSize);
                               Toast.makeText(getActivity(), item.getScreenName(),Toast.LENGTH_LONG).show();
                           }
                        }
                    });
                }
            }
        });
    

    Hope it helps!!

    Login or Signup to reply.
  2. try this:

        mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
    
                    if (twitterFollowers.get(position).getId() != 0){
                        loadOtherFollowers(twitterFollowers.get(position).getId());
    
                    }
    
                }
    
            });
    
    
    
    
    private void loadOtherFollowers(long id) {
        Log.e("ID","id is "+id);
        MyTwitterApiClient myTwitterApiClient2 = new MyTwitterApiClient(twitterSession);
        myTwitterApiClient2.getCustomTwitterService2().list(id).enqueue(new retrofit2.Callback<FollowersResponseModel>() {
            @Override
            public void onResponse(Call<FollowersResponseModel> call, Response<FollowersResponseModel> response) {
                Log.e("onResponse",response.toString());
                otherFollowers = fetchResults2(response);
                Log.e("onResponse","otherFollowers:" +otherFollowers.size());
                        MyCustomAlertDialog(twitterFollowers.get(position).getProfilePictureUrl(), otherFollowers.size());
                        Toast.makeText(getActivity(), twitterFollowers.get(position).getScreenName(),Toast.LENGTH_LONG).show();
                        //sendMsg(twitterFriends.get(position).getId(),twitterFriends.get(position).getScreenName(),"");
            }
    
            @Override
            public void onFailure(Call<FollowersResponseModel> call, Throwable t) {
                Log.e("onFailure",t.toString());
            }
    
        });
    
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search