Paul
Paul

Reputation: 1734

Arraylist indexoutofbounds exceptions

So I'm just making a small little app for personal reasons. Basically all it does is parse xml from a website and it will a return a list of colors, title, & author and then display it all in a listview. It kind of works but sometimes I will get these indexoutofboundsexceptions e.g that always occur in the for loop here. To explain the for loop a bit better. result is an arraylist. Each row in the listview is made up of 3 elements: the title, username, and color. The for loop iterates through the array basically assigning the values to the row so initially the first element will be assigned to title, the 2nd the username and so on. The pattern just repeats itself.If anyone sees the problem or even knows of a better way to do this please share :)

   for (int i = 0; i < result.size(); i++) {
       sr = new SearchResults();
       sr.setTitle(result.get(i));
       i++;
       sr.setUser(result.get(i));
       i++;
       sr.setHex(result.get(i));
       results.add(sr);
   }

The error usually looks something along these lines:

07-11 14:48:08.272: E/AndroidRuntime(15370): FATAL EXCEPTION: main
07-11 14:48:08.272: E/AndroidRuntime(15370): java.lang.IndexOutOfBoundsException: Invalid index 62, size is 62

Here is the code:

public class ColorsActivity extends ListActivity {
    /** Called when the activity is first created. */
    private int selection = 0;
    private String info;
    private ArrayList<String> myArr = new ArrayList<String>();
    private int resultOffset=0;
    private TextView settings;
    private SearchResults sr = new SearchResults();
    private ArrayList<SearchResults> results = new ArrayList<SearchResults>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        settings = (TextView) findViewById(R.id.settings_text);

        final ListView lv = getListView();
        lv.setDividerHeight(0);

        colorTask refTask = new colorTask(); 
        refTask.execute();

        settings.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                colorTask refTask = new colorTask(); 
                refTask.execute();
            }
        });  
    }

    public class MyCustomBaseAdapter extends BaseAdapter {
        private ArrayList<SearchResults> searchArrayList;

        private LayoutInflater mInflater;

        public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) {
            searchArrayList = results;
            mInflater = LayoutInflater.from(context);
        }

        public int getCount() {
            return searchArrayList.size();
        }

        public Object getItem(int position) {
            return searchArrayList.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list_item, null);
                holder = new ViewHolder();
                holder.txtHex = (TextView) convertView.findViewById(R.id.text1);
                holder.txtTitle = (TextView) convertView.findViewById(R.id.TextView02);
                holder.txtUser = (TextView) convertView.findViewById(R.id.TextView01);
                holder.txtColorValue = (TextView) convertView.findViewById(R.id.TextView03);

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

            holder.txtTitle.setText(searchArrayList.get(position).getTitle());
            holder.txtUser.setText(searchArrayList.get(position).getUser());
            String c = searchArrayList.get(position).getHex();
            holder.txtHex.setBackgroundColor(Integer.parseInt(c, 16)+0xFF000000);
            holder.txtColorValue.setText(searchArrayList.get(position).getHex());

            return convertView;
        }

        class ViewHolder {
            TextView txtTitle;
            TextView txtUser;
            TextView txtHex;
            TextView txtColorValue;
        }
    }

    class colorTask extends AsyncTask<String, Void, ArrayList<String>> {
        private final ProgressDialog dialog = new ProgressDialog(ColorsActivity.this);

        // can use UI thread here
        protected void onPreExecute() {
           this.dialog.setMessage("Contacting server...");
           this.dialog.show();
        }

        protected ArrayList<String> doInBackground(final String... args) {
            URL url = null;
            ParsedExampleDataSet parsedExampleDataSet = null;
            try {                       
                if (selection == 0) {
                     url = new URL ("http://www.colourlovers.com/api/colors/top?resultOffset=" + resultOffset);
                } 

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                SAXParserFactory spf = SAXParserFactory.newInstance();
                SAXParser sp = null;
                try {
                    sp = spf.newSAXParser();
                } catch (ParserConfigurationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SAXException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                /* Get the XMLReader of the SAXParser we created. */
                XMLReader xr = null;
                try {
                    xr = sp.getXMLReader();
                } catch (SAXException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                /* Create a new ContentHandler and apply it to the XML-Reader*/
                getColors myExampleHandler = new getColors(selection);
                xr.setContentHandler(myExampleHandler);

                /* Parse the xml-data from our URL. */
                try {
                    xr.parse(new InputSource(url.openStream()));
                    parsedExampleDataSet =
                        myExampleHandler.getParsedData();
                    myArr = parsedExampleDataSet.toArrayList();             
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SAXException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }   
                    return myArr;               
        }

        // can use UI thread here
        protected void onPostExecute(final ArrayList<String> result) {
           if (this.dialog.isShowing()) {
              this.dialog.dismiss();
           }
           if (result != null) {    
               for (int i = 0; i < result.size(); i++) {
                   sr = new SearchResults();
                   sr.setTitle(result.get(i));
                   i++;
                   sr.setUser(result.get(i));
                   i++;
                   sr.setHex(result.get(i));
                   results.add(sr);
               }
               ListView lv = getListView();
               lv.setAdapter(new MyCustomBaseAdapter(ColorsActivity.this, results));
               resultOffset += 20;

           } else {
               Toast.makeText(ColorsActivity.this, "Error contacting server.", Toast.LENGTH_SHORT).show();
           }
        }

     }
    public class SearchResults {
         private String title = "";
         private String hex = "";
         private String user = "";

         public void setTitle(String title) {
          this.title = title;
         }

         public String getTitle() {
          return title;
         }

         public void setHex(String hex) {
          this.hex = hex;
         }

         public String getHex() {
          return hex;
         }
         public void setUser(String user) {
           this.user = user;
         }

         public String getUser() {
           return user;
         }
    }

Upvotes: 0

Views: 251

Answers (4)

Jesse M
Jesse M

Reputation: 175

The issue is that you are trying to access an index in your arraylist that does not exist. The problem is definitely inside the for loop:

for (int i = 0; i < result.size(); i++) {
   sr = new SearchResults();
   sr.setTitle(result.get(i));
   i++;
   sr.setUser(result.get(i));
   i++;
   sr.setHex(result.get(i));
   results.add(sr);
}

This would be fine if you only incremented i in the loop declaration; it will be with one of the two other i++ statements that the problem will occur, since after each you are not checking if the length of the array has been exceeded. This must mean that in one or more cases, there is a triplet (Title, User, Hex) that is missing one or more of its parameters. If there should ALWAYS be all three of the parameters present, then the array isn't populating correctly.

If you want the code to be failsafe in the case of a mismatched parameter, then check i against the length of the array every time you increment it, i.e. twice more inside the loop-- then, if it has reached the array's length, handle the error in whatever way you see fit (break out of the loop, fill the remaining values of sr with dummy data, etc).

Something like this:

for (int i = 0; i < result.size(); i++) {
   sr = new SearchResults();
   sr.setTitle(result.get(i));

   i++;
   if (i == result.size()) {
      sr.setUser("dummy");
   }
   else {
      sr.setUser(result.get(i));
   }

   i++;
   if (i == result.size()) {
      sr.setHex("#000000");
   }
   else {
      sr.setHex(result.get(i));
   }

   results.add(sr);
}

Upvotes: 0

cklab
cklab

Reputation: 3771

for (int i = 0; i < result.size(); i++) {
    sr = new SearchResults();
    sr.setTitle(result.get(i));
    i++;
    sr.setUser(result.get(i));
    i++;
    sr.setHex(result.get(i));
    results.add(sr);
}

You blindly increment i here. This is most likely the issue. Getting to the root of the problem, it's likely that the data you are parsing is either sometimes incomplete or incorrectly parsed such that it doesn't always produce your desired number of parameters (i.e. you parse something expecting title, user, and color, but only get a title and a user).

Since the size of the List is 62, this suggest you missed at least 1 parameter (either a title, user, or color) when parsing. If all was parsed correctly, your List would have had a size of 63.

Upvotes: 2

Joel
Joel

Reputation: 4772

Your problem is in your for loop. You are increasing the variable i inside of the loop. So when i is 61 when size is 62, you add one too i, trying to access index 62, when the size is 62.

Upvotes: 0

barbiepylon
barbiepylon

Reputation: 911

When you increase i (i++) you are going past the bounds of your array since you are iterating from 0-size of the array. When you get to size-2 then the second i++ inside the for loop will be an index that does not exist.

Upvotes: 0

Related Questions