0

First of all thanks for reading my question.

Right now i am implementing the web services to my android app using OKHTTP. App is running fast if i am using Fast internet. In slow internet I got data slowly thats also fine. But issue is if i switch one activity to another activity without waiting for data in response of data call. At that time it is not able to find the current control and throw the exception NullPointer. what to do in this type of situation to manage the NullPointer Error.

It find the previous activity ui control in Current activity to set data.

FATAL EXCEPTION: OkHttp Dispatcher
                                               Process: in.cess, PID: 16535
                                               java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.FragmentActivity.runOnUiThread(java.lang.Runnable)' on a null object reference
                                                   at in.cess.fragment.WorkRequestCompletedFragment.onResponse(WorkRequestCompletedFragment.java:221)
                                                   at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:177)
                                                   at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
                                                   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                   at java.lang.Thread.run(Thread.java:818)

Activity Code for response.

this.getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mAdapter = new CallAdapter(getActivity(),movieList,
                                new CallAdapter.OnItemClickListener() {
                                    @Override
                                    public void onFeedClicked(CallDeatails jobDetails) {
                                        Intent i = new Intent(getActivity(), SalesCallDetailsActivity.class);
                                        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                        i.putExtra(Const.INTENT_DATA,jobDetails);
                                        i.putExtra(Const.INTENT_TODAY,"true");
                                        getActivity().startActivity(i);

                                    }
                                });
                        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
                        recyclerView.setLayoutManager(mLayoutManager);
                        recyclerView.setItemAnimator(new DefaultItemAnimator());
                        recyclerView.setAdapter(mAdapter);

                        txt_total_call.setText("Total Call : "+String.valueOf(movieList.size()));
                    }
                });
  • @AkashPatel Its not duplicate. It find the previous activity ui control in Current activity to set data. – Krupa Rudani Feb 27 '17 at 07:13
  • 1
    post error log and show code where you are getting this error. – Akash Patel Feb 27 '17 at 07:16
  • 1
    You should stop your all the background processes in your `onStop()` of the current running activity and also free all the resources that are referenced in your process. – phoenix Feb 27 '17 at 07:18
  • @Mr.Rabbit how can i stop okhttp calla and free resource?? – Krupa Rudani Feb 27 '17 at 07:20
  • 2
    @krupapatel For cancel: https://github.com/square/okhttp/wiki/Recipes#canceling-a-call and also in your response handling method you can have a check like `if(!this.getActivity().isFinishing()) // handle the response` – phoenix Feb 27 '17 at 07:22

0 Answers0