0

When I worked in network wifi the program behaves perfectly. but if I'm in a network of mobile Internet, we often get the error. it occurs at different times in different fragments, but always in the same place.

this is my AsyncTask:

public class MyAsincTask extends AsyncTask<String, Void, Document> {
    private Document document;
    private final ProgressDialog progressDialog;

    public MyAsincTask(Context context) {
        progressDialog = MyProgress.getProgressDialog(context);
    }

    @Override
    protected void onPreExecute() {
        progressDialog.show();
        super.onPreExecute();
    }

    @Override
    protected Document doInBackground(String... params) {
        try {
            document = Jsoup.connect(params[0]).get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return document;
    }

    @Override
    protected void onPostExecute(Document document) {
        progressDialog.cancel();
        super.onPostExecute(document);
    }
}

so I call it:

 public void setData(){
        new MyAsincTask(getActivity()){
            @Override
            protected void onPreExecute() {
                count=0;
            }
            @Override
            protected void onPostExecute(Document document) {
                super.onPostExecute(document);
                guests = new ArrayList<>();
                Elements elementsUserId = document.select("guest_id");
                Elements elementsNumbers = document.select("room");



                for (int i = 0; i < elementsNumbers.size(); i++) {
                    GuestBean guest = new GuestBean();
                    guest.setUserId(elementsUserId.get(i).ownText());
                    guest.setRoom(elementsNumbers.get(i).ownText());                  
                    guests.add(guest);
                    count++;
                }
                if (getActivity()!=null) {
                    GuestsListAdapter adapter = new GuestsListAdapter(guests);

                    list.setAdapter(adapter);
                    progressDialog.cancel();
                    swipeLayout.setRefreshing(false);
                    header = "Гости " + hotel + "" + "(" + count + ")";
                    getActivity().setTitle(header);
                }
            }
        }.execute(link);
    }

when I turn on the fragments sometimes get this error. it happens at different fragments but always in the same area code:

01-06 19:55:49.765  11237-11237/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.managment.pavel.managmentgradle.fragments.GuestsList$1.onPostExecute(GuestsList.java:100)
            at com.managment.pavel.managmentgradle.fragments.GuestsList$1.onPostExecute(GuestsList.java:91)
            at android.os.AsyncTask.finish(AsyncTask.java:602)
            at android.os.AsyncTask.access$600(AsyncTask.java:156)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4464)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:822)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:589)
            at dalvik.system.NativeStart.main(Native Method)

line 91:

new MyAsincTask(getActivity()){

line 100

Elements elementsUserId = document.select("guest_id");
  • Its easy. Your `document` is null, therefore it means, it doesnt exist. Mobile Internet is very unstable, so it might be an timeout. Try This: `document = Jsoup.connect(params[0]).timeout(0).get();` http://jsoup.org/apidocs/org/jsoup/Connection.html#timeout(int). – Vojtěch Pešek Jan 06 '15 at 14:19
  • Do you have a way of detecting if you have a stable connection to the web? See this: http://stackoverflow.com/questions/4238921/detect-whether-there-is-an-internet-connection-available-on-android – Mr. Concolato Jan 06 '15 at 14:19
  • 1
    @Mr. Concolato I have a check for the Internet. except that if it's slow it's me I can not check – Pavel Petrashov Jan 06 '15 at 14:24
  • @Vojtaaa9 now check why timeout(0)? maby timeout(100 || 200 || ....) – Pavel Petrashov Jan 06 '15 at 14:26
  • @PavelPetrashov first check if it works with 0, if it does it's a timeout problem, because 0 = no timeout. – Simas Jan 06 '15 at 14:29
  • ahhh you think that it gives an exception Jsoup Because it takes 3 seconds and he did not have time to download the data? I did. now have to wait. Is there a mistake. will check – Pavel Petrashov Jan 06 '15 at 14:31
  • how about checking 1-`document!=null`:before proceed, 2-`document.select("guest_id")!=null`:before proceed, 3-`document.select("room")!=null`:before proceed. Even if any problems occurs with connection, then you will make sure you have valid `document` and `element`, so you will not have a crash.... and add logs if case any NULL detected – Yazan Jan 06 '15 at 14:40
  • @Vojtaaa9 Unfortunately an error has not disappeared – Pavel Petrashov Jan 06 '15 at 14:47
  • @ Yazan Well, let's do a document is NULL. How do I fix the code? – Pavel Petrashov Jan 06 '15 at 14:51
  • yes check showed that document is NULL. How do I fix the code? – Pavel Petrashov Jan 06 '15 at 15:06

0 Answers0