Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

in my project I have to set in a normal Activity multiple ListView (the number of these ListView depend of a size of an ArrayList).

So I have to programmatically add dynamically ListView and Custom ArrayAdapter.

I had th idea to make an AsyncTask to do these dirty stuffs : making an array of custom ArrayAdapter and to set dynamically a ListView for each Adapter in the array.

But the main problem I have is when I just try to do MyAdapterArray.add(MyAdapter) ... I have a Null Pointer Exception :

03-15 15:24:21.738: E/AndroidRuntime(21672): FATAL EXCEPTION: main
03-15 15:24:21.738: E/AndroidRuntime(21672): java.lang.RuntimeException: Unable to start activity ComponentInfo{azur.mobile.incomrestau/azur.mobile.incomrestau.SousCarteActivity}: java.lang.NullPointerException
03-15 15:24:21.738: E/AndroidRuntime(21672):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
03-15 15:24:21.738: E/AndroidRuntime(21672):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
03-15 15:24:21.738: E/AndroidRuntime(21672):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-15 15:24:21.738: E/AndroidRuntime(21672):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
03-15 15:24:21.738: E/AndroidRuntime(21672):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-15 15:24:21.738: E/AndroidRuntime(21672):    at android.os.Looper.loop(Looper.java:137)
03-15 15:24:21.738: E/AndroidRuntime(21672):    at android.app.ActivityThread.main(ActivityThread.java:4898)
03-15 15:24:21.738: E/AndroidRuntime(21672):    at java.lang.reflect.Method.invokeNative(Native Method)
03-15 15:24:21.738: E/AndroidRuntime(21672):    at java.lang.reflect.Method.invoke(Method.java:511)
03-15 15:24:21.738: E/AndroidRuntime(21672):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
03-15 15:24:21.738: E/AndroidRuntime(21672):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
03-15 15:24:21.738: E/AndroidRuntime(21672):    at dalvik.system.NativeStart.main(Native Method)
03-15 15:24:21.738: E/AndroidRuntime(21672): Caused by: java.lang.NullPointerException
03-15 15:24:21.738: E/AndroidRuntime(21672):    at azur.mobile.incomrestau.SousCarteActivity.onCreate(SousCarteActivity.java:43)
03-15 15:24:21.738: E/AndroidRuntime(21672):    at android.app.Activity.performCreate(Activity.java:5206)
03-15 15:24:21.738: E/AndroidRuntime(21672):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
03-15 15:24:21.738: E/AndroidRuntime(21672):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
03-15 15:24:21.738: E/AndroidRuntime(21672):    ... 11 more

My Activity is here :

public class SousCarteActivity extends Activity {
    private String nom_categorie;
    private ArrayList<String> arraySousCategoriesName;
    private ArrayList<ArrayList<ElementFood>> arraySousCategories;
    private ArrayList<MyAdapter> myAdapters;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_souscarte_list_item);

        nom_categorie = getIntent().getStringExtra("nom_categorie");

        FoodsContainer.setSousCategoriesNameArray(nom_categorie);
        FoodsContainer.setSousCategoriesArray(nom_categorie);

        arraySousCategories = FoodsContainer.sousCategoriesArray;

        MyAdapter adapter = new MyAdapter(this, arraySousCategories.get(0));
        myAdapters.add(0, adapter);

        //new DoDirtyJobAsyncTask().execute();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    private class DoDirtyJobAsyncTask extends AsyncTask<Void, MyAdapter, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            for (ArrayList<ElementFood> arrayElement : arraySousCategories) {
                MyAdapter myAdapter = new MyAdapter(getApplicationContext(), arrayElement);
                myAdapters.add(myAdapter);
                publishProgress(myAdapter);
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(MyAdapter... myAdapters) {
            int currViewId = 1;
            for (final MyAdapter myAdapter: myAdapters) {
                ListView listview = new ListView(getApplicationContext(), null);
                listview.setId(currViewId);
                listview.setAdapter(myAdapter);

                LinearLayout ll = (LinearLayout) findViewById(R.id.souscarte_linearlayout);
                ll.addView(listview);
                currViewId++;
            }
        }

    }

    class MyAdapter extends ArrayAdapter<ElementFood> 
    {
            LayoutInflater inflat;
            private ArrayList<ElementFood> items;

            public MyAdapter(Context context, ArrayList<ElementFood> objects) 
            {
                super(context, R.layout.activity_souscarte_list_item_elementsouscategorie, objects);
                this.items = objects;
                this.inflat = LayoutInflater.from(context);
            }
           private class ViewHolder{
                public TextView title;
                public TextView prix;
                public TextView desc;
            }

           @Override
           public View getView(int position, View convertView, ViewGroup parent) {
               ViewHolder holder = null;
               if (convertView == null) {
                       holder = new ViewHolder();
                       convertView = inflat.inflate(R.layout.activity_souscarte_list_item_elementsouscategorie, null);
                       holder.title = (TextView) convertView.findViewById(R.id.souscarte_element_title);
                       holder.prix =  (TextView) convertView.findViewById(R.id.souscarte_element_prix);
                       holder.desc =  (TextView) convertView.findViewById(R.id.souscarte_element_desc);

                       convertView.setTag(holder);
               } else {
                       holder = (ViewHolder) convertView.getTag();
               }
               ElementFood element = items.get(position);
               if (element != null) {
                   holder.title.setText(element.getName());
                   holder.prix.setText(Float.toString(element.getPrice()));
                   holder.desc.setText(element.getDescription());
               }
               return convertView;
           }
    }
}

PS : My array "arraySousCategories" is good, and the Adapter is working fine when I use it normally (without the Adapter array)..

Thank you

share|improve this question
What's line 43? Without the full file we can't really guess. But something on that line is dereferencing a null. – Gabe Sechan Mar 15 at 14:34

2 Answers

up vote 0 down vote accepted

You probably want an ArrayList<MyAdapter> myAdapters = new ArrayList<MyAdapter>() before you call myAdapters.add(0, adapter) otherwise myAdapters is null

share|improve this answer
Ahah !! What's a joke, Friday is too hard for me XD thank you Anton ;) – eento Mar 15 at 14:45

At this line myAdapters.add(0, adapter); in your onCreate, myAdapters is null.

share|improve this answer
Lol Useless post, I can read too.. – eento Mar 15 at 14:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.