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 8206809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:47:53+00:00 2026-06-07T08:47:53+00:00

So I’m just making a small little app for personal reasons. Basically all it

  • 0

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;
         }
    }
  • 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-07T08:47:57+00:00Added an answer on June 7, 2026 at 8:47 am

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
In my XML file chapters tag has more chapter tag.i need to display chapters
I have a French site that I want to parse, but am running into

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.