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
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:
and after this modify this method like so:
and then in your list it will be something like this:
Hope it helps!!
try this: