Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8662417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:44:00+00:00 2026-06-12T16:44:00+00:00

I have several stacktraces like the ones below all over my app anytime there

  • 0

I have several stacktraces like the ones below all over my app anytime there is a call to get data from my MySQL DB. If the connection goes out and it timeouts, I get the following stacktraces:

Example:

java.lang.NullPointerException
at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
at com.---.---.profile.MyReviewAdapter.<init>(MyReviewAdapter.java:20)
at com.---.---.profile.UserFragmentActivity$CommentFragment$MyCommentTask.onPostExecute(UserFragmentActivity.java:559)
at com.---.---.profile.UserFragmentActivity$CommentFragment$MyCommentTask.onPostExecute(UserFragmentActivity.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

Example:

java.lang.NullPointerException
at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
at com.---.---.master.TopItemAdapter.<init>(TopItemAdapter.java:18)
at com.---.---.master.MainFragmentActivity$TopFrag$TopTask.onPostExecute(MainFragmentActivity.java:961)
at com.---.---.master.MainFragmentActivity$TopFrag$TopTask.onPostExecute(MainFragmentActivity.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

Here is a sample code that is being called with this second LogCat:

class TopTask extends AsyncTask<String, String, Void> {
            private MyProgressDialog progressDialog = new MyProgressDialog(
                    getActivity());
            InputStream is = null;
            String result = "";

            protected void onPreExecute() {

                    progressDialog.setMessage("Loading...");
                    progressDialog.show();
                    progressDialog.setOnCancelListener(new OnCancelListener() {

                        public void onCancel(DialogInterface dialog) {
                            TopTask.this.cancel(true);

                        }
                    });

            }

            @Override
            protected Void doInBackground(String... params) {
                String url_select = "http://www.---.com/---/master_cat_items.php";

                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url_select);
                ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
                param.add(new BasicNameValuePair("Sort", Sort));

                try {
                    httpPost.setEntity(new UrlEncodedFormEntity(param));
                    HttpResponse httpResponse = httpClient.execute(httpPost);
                    HttpEntity httpEntity = httpResponse.getEntity();

                    // read content
                    is = httpEntity.getContent();

                } catch (Exception e) {

                    Log.e("log_tag", "Error in http connection " + e.toString());
                }
                try {
                    BufferedReader br = new BufferedReader(
                            new InputStreamReader(is));
                    StringBuilder sb = new StringBuilder();
                    String line = "";
                    while ((line = br.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    is.close();
                    result = sb.toString();

                } catch (Exception e) {
                    // TODO: handle exception
                    Log.e("log_tag", "Error converting result " + e.toString());
                }

                return null;

            }

            protected void onPostExecute(Void v) {

                String item, cat;
                try {
                    JSONArray jArray = new JSONArray(result);
                    JSONObject json_data = null;
                    for (int i = 0; i < jArray.length(); i++) {
                        json_data = jArray.getJSONObject(i);
                        item = json_data.getString("item");
                        cat = json_data.getString("category");

                        items.add(item);
                        cats.add(cat);

                    }
                } catch (JSONException e1) {

                } catch (ParseException e1) {
                    e1.printStackTrace();
                } catch (Exception e) {
                                   e.printStackTrace(); 
                                   // toast to notify user to reload

                            }



                TopItemAndTrendingObject[] mco = new TopItemAndTrendingObject[items
                        .size()];
                int index = 0;

                for (@SuppressWarnings("unused")
                String i : items) {
                    mco[index] = new TopItemAndTrendingObject(items.get(index),
                            cats.get(index));
                    index++;
                }

                adapter = new TopItemAdapter(getActivity(), mco); // this is what is referenced in LogCat
                setListAdapter(adapter);


                progressDialog.dismiss();


            }
        }

So my questions are:

  1. What does this exception mean(yes, I know what a NullPointerException is)?
  2. More importantly, how can I prevent this? Even if it is as basic as throwing a toast when connection drops(anything instead of a force close?

Edit: Code for TopItemAdapter as requested

public class TopItemAdapter extends ArrayAdapter<TopItemAndTrendingObject> {
    private final Context context;

    public TopItemAdapter(Context context, TopItemAndTrendingObject[] items) {
        super(context, 0, items);
        this.context = context;

    }

    static class ViewHolder {
        public TextView tv1;
        public TextView tv2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        View rowView = convertView;
        if (rowView == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            rowView = inflater
                    .inflate(R.layout.master_cat_all_time, null, true);

            holder = new ViewHolder();
            holder.tv1 = (TextView) rowView.findViewById(R.id.myMastCat_Cat);
            holder.tv2 = (TextView) rowView.findViewById(R.id.myMasterCat_Item);

            rowView.setTag(holder);
        } else {
            holder = (ViewHolder) rowView.getTag();
        }

        TopItemAndTrendingObject mco = getItem(position);
        String item = mco.item;
        String cat = mco.cat;

        holder.tv2.setText(String.valueOf(position + 1) + ". " + item);
        holder.tv1.setText(cat);

        return rowView;

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:44:01+00:00Added an answer on June 12, 2026 at 4:44 pm

    What does this exception mean

    As I already said in my comments that NullPointerException doesn’t appear because the database connection fails(in which case a different error would have occurred and also much faster), it appears because you didn’t take in consideration the way Android uses activities and fragments(more precisely their lifecycle).

    I don’t know if you have extra details about those exceptions, my guess is that the exception appears when a user goes to the problematic Fragment sees the ProgressDialog(and the task is started) and then rotates the phone. At this moment Android will recreate the activity along with any of the containing fragments. This should be handled by your code but, instead, you did something in particular bad, that will throw the NullPointerException.

    Looking at the way you declared the TopTask class is safe to say it’s an inner class in the Fragment. Being an inner class means that it has a “connection” to the enclosing Fragment instance allowing it to use the fragment’s methods(like getActivity()). So the user goes to the Fragment, the task is started and then rotates the phones. At this time the activity and the fragments will be recreated, but your task(which is still running) will keep the special reference to the initial fragment(which is dead by now). After rotation this initial fragment reference will be detached from an activity and its getActivity method will return null. Passing null to an ArrayAdapter(in your case through the a super call) will throw the exceptions you see(which come from a device with ICS). The array that you pass to the adapter can’t be null as you declare it right above the adapter’s initialization. This will also throw a different exception.

    More importantly, how can I prevent this? Even if it is as basic as throwing a toast when connection drops(anything instead of a force close?

    The simplest thing to do is to not destroy the fragments along configuration changes. This is done by using the setRetainInstance(true) method of a Fragment so the reference to the Fragment will be kept valid(in the onCreate callback for the Fragment for example). I would also make the TopTask class as a static class and pass a reference to the Fragment in the constructor of the TopTask. This should be tested.

    You could also send a reference to the application context from the Fragment to the TopTask like this(but maybe you should avoid it) which will be valid:

    // in the onActivityCreated method
    new TopTask(getActivity().getApplicationContext()).execute(/*parameters*/);
    

    Also keep the reference for the ProgressDialog but initialize it in the onPreExecute callback.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several objects, like products, order, etc. When I get information from my
I have several UITableViews and what I would like to do is to be
I have several listviews with two collumns like. I am building a stats application
I have several (as in 100s) spreadsheets using an addin (let's call it Addin2003).
I have several hosts from which i want to do the same query. So
I have several LESS files that are all imported into one master file (styles.less).
I have searched on google and stackoverflow for this problem there are several questions
I have several a tags as shown below on a dynamically generated page <a
I have several resource dictionaries with theme-related data, where I declared styles for particular
I have several tables/variables (sampled below): mytable = { ['100'] = { ['2']=0,['3']=0,['5']=0,['6']=0,['7']=0,['9']=0}, ['101']

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.