0

I am working on the sax XML parsing.

Logcat error like...

java.lang.IndexOutOfBoundsException: Invalid index 11, size is 11 

I got the error at

map.put("pubdate", sitesList.getPubdate().get(i));

for (int i = 0; i < sitesList.getName().size(); i++) {
        System.out.println("value of i==============>"+i);
        HashMap<String, String> map = new HashMap<String, String>();

        System.out.println("\nvalue of title==============>"+ sitesList.getName().get(i));

        map.put("title", sitesList.getName().get(i));
        map.put("pubdate", sitesList.getPubdate().get(i));
        map.put("desc", sitesList.getDesc().get(i));

         items.add(map);

    }

Thanks everyone.

nfechner
  • 17,295
  • 7
  • 45
  • 64
Rahul Patel
  • 3,823
  • 5
  • 30
  • 46

4 Answers4

2

Are you sure that the sitesList.getName() has the same size of sitesList.getPubdate()?

Because the only reason for that exception is that sitesList.getName() is bigger than sitesList.getPubdate() :)

StErMi
  • 5,389
  • 5
  • 48
  • 71
  • Thanks,Yes You are right,sitesList.getName() is bigger than sitesList.getPubdate(). How can i solve for that. – Rahul Patel May 31 '12 at 10:33
  • 1
    Well now you have to understand why they have different size and correct that problem :) To solve that problem you can check which list has the min size and use that size as guard for your for loop but with this solution you will miss some data if you have a larger list (as in your case). As I said, before, try to understand why you have different sizes – StErMi May 31 '12 at 10:36
  • I am working for the SAX xml parsing for the "http://news.google.com/?output=rss" getname() is value of the title and getpubdate() is value of the date. – Rahul Patel May 31 '12 at 10:40
1

i think here size of sitesList,

Use sitesList.size() instead of sitesList.getName().size()

for (int i = 0; i < sitesList.size(); i++) {

        System.out.println("value of i==============>"+i);
        HashMap<String, String> map = new HashMap<String, String>();

        System.out.println("\nvalue of title==============>"+ sitesList.get(i).getName());

        map.put("title", sitesList.get(i).getName());
        map.put("pubdate", sitesList.get(i).getPubdate());
        map.put("desc", sitesList.get(i).getDesc());

        items.add(map);

    }
assylias
  • 321,522
  • 82
  • 660
  • 783
Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
0

Souldnt your loop look like:

for (int i = 0; i < sitesList.size(); i++) {
        System.out.println("value of i==============>"+i);
        HashMap<String, String> map = new HashMap<String, String>();

        System.out.println("\nvalue of title======>"+ sitesList.get(i).getName());

        map.put("title", sitesList.get(i).getName());
        map.put("pubdate", sitesList.get(i).getPubdate());
        map.put("desc",sitesList.get(i).getDesc());

         items.add(map);

    }
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
0

Thanks to all, Finally I got the solution For my problem, Here the sitesList.getName() is bigger than sitesList.getPubdate() , so Now I use

for (int i = 0; i < sitesList.getPubdate().size(); i++) in place of
for (int i = 0; i < sitesList.getName().size(); i++)

for (int i = 0; i < sitesList.getPubdate().size(); i++) {
        System.out.println("value of i==============>"+i);
        HashMap<String, String> map = new HashMap<String, String>();


        map.put("title", sitesList.getName().get(i));
        map.put("pubdate", sitesList.getPubdate().get(i));
        map.put("desc", sitesList.getDesc().get(i));

         items.add(map);
    }
Rahul Patel
  • 3,823
  • 5
  • 30
  • 46