-4

I want to run through an ArrayList<Object> which contains some ArrayList of different types. But each type is extended by ItemProperties with basic information about the type like name and picture (.getName() and .getPicture()).

ArrayList<Object> lists = new ArrayList<Object>();
lists.add(listType1); //Class Type1 extends ItemProperties
lists.add(listType2); //Class Type2 extends ItemProperties
lists.add(listType3); //Class Type3 extends ItemProperties

for (Object o : lists) {
    ArrayList<ItemProperties> al = (ArrayList<ItemProperties>) o;
    for (ItemProperties p : al) { //Nullpointer Exception here
        if (getString(p.getName()).toLowerCase().contains(arg0.toLowerCase())) {
            Drawable icon = getResources().getDrawable(p.getPicture());
            TextView tv = new TextView(this);
            tv.setTextSize(20);
            tv.setTextColor(Color.WHITE);
            tv.setGravity(Gravity.CENTER_VERTICAL);
            tv.setText(p.getName());
            ll.addView(tv); //LinearLayout ll
            icon.setBounds(0, 0, tv.getLineHeight() * 3, tv.getLineHeight() * 3);
            tv.setCompoundDrawables(icon, null, null, null);
        }
    }

How can I get this to work?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kashan
  • 1
  • 2

3 Answers3

1

The NullPointerException will not have anything to do with the way you have set up your lists. However, if you what you say is correct, that the first list will contain lists which in turn will all contain elements extending ItemProperties then you should type your lists for that.

List<List<ItemProperties>> lists = new ArrayList<List<ItemProperties>>();
Nicklas Gnejs Eriksson
  • 3,395
  • 2
  • 21
  • 19
0

The NullPointerException occurs when you declare a variable but did not create an object. This exception occurs when you try to use a reference that points to no location in memory as though it were referencing an object.

NullPointerException is itself enough to tell you that you haven't initialized the ArrayList properly. If you still have a problem, post stackTrace().

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adarsh Singhal
  • 362
  • 1
  • 3
  • 12
-1

I have a few suggestions for improving your current code. Rather than defining a List of Objects to hold your lists, you can type it more strongly by using the following syntax:

ArrayList< ArrayList<? extends ItemProperties> > lists = new ArrayList<>();

So now when you loop through your list you don't have to type cast your elements in the for-each loop.

 for (ArrayList<? extends ItemProperties> o : lists) {
     // Do something
 }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kakarot
  • 4,252
  • 2
  • 16
  • 18
  • Thanks for the tip but some of my list are multiple extended. For example: Type1 extends xy extends ItemProperties. My code actually works fine at the moment. I just need to figure out now how i can open them with onClick. – Kashan May 14 '14 at 16:11
  • even if its multiple extended this idiom works in your case as long as you only want to use the methods defined in ItemProperties inside your for loop – Kakarot May 14 '14 at 16:14
  • 1) You are describing the wrong list, the first list contains lists, which in his example are ArrayList. 2) You cannot use extends ItemProperties> in this way for this functionality, since it would not allow you to add the elements, using just ItemProperties as I described would be the proper way. More info: http://stackoverflow.com/questions/2723397/java-generics-what-is-pecs – Nicklas Gnejs Eriksson May 15 '14 at 07:09
  • I hope you read my edited comment since the example you provided will not even compile if you try to add something to the extend X> list. – Nicklas Gnejs Eriksson May 15 '14 at 07:28
  • @Nicklas Gnejs Eriksson Thanks a lot for your comment. I changed the code to reflect it. Sorry for adding this omment so late, as I modified the code in sleep mode :P – Kakarot May 15 '14 at 14:54