Loading...
Logo
Processing Forum
Hi Everyone,

I am fairly new to processing I am doing a project at school that I need to grab two different rss feeds into my processing sketch. I feel I was on the right track until I ran into ArrayIndexOutOfBoundsException:0 error on this line..

String imageURL2 = imageURLElements2[q].getStringAttribute("url");

 Can someone have a look over my code?

/**
 * Displays titles in a color picked from the center of the thumbnail.
 * Images and headlines scroll over the screen.

 */

import processing.opengl.*;
import processing.xml.*;

// The items array
Item[] items;
Item2[] items2;
// 20 items can be displayed
int itemNumber = 20;
int itemNumber2 = 20;

PFont font;

void setup() {
  size(1000, 950, OPENGL);
  font = loadFont("Consolas-BoldItalic-42.vlw");
  textFont(font, 42);

  // Loads RSS feed and gets title and image of each item
XMLElement rss = new XMLElement(this, "http://www.digg.com/rss/index.xml");
XMLElement[] titleXMLElements = rss.getChildren("channel/item/title");
XMLElement[] imageURLElements = rss.getChildren("channel/item/media:thumbnail");
 
items = new Item[itemNumber];
//Gets title, loads image and creates Item for each feed item
for (int i = 0; i < items.length; i++) {
String title = titleXMLElements[i].getContent();
String imageURL = imageURLElements[i].getStringAttribute("url");
PImage img = loadImage(imageURL);
items[i] = new Item(title, img, 10, i * 50);
  }
 
XMLElement rss2 = new XMLElement(this, "http://rss.cbc.ca/lineup/topstories.xml");
XMLElement[] titleXMLElements2 = rss2.getChildren("channel/item/title");
XMLElement[] imageURLElements2 = rss2.getChildren("channel/item/media:thumbnail");
 
items2 = new Item2[itemNumber2];
  // Gets title, loads image and creates Item for each feed item
for (int q = 0; q < items2.length; q++) {
String title2 = titleXMLElements2[q].getContent();
String imageURL2 = imageURLElements2[q].getStringAttribute("url");
PImage img2 = loadImage(imageURL2);
items2[q] = new Item2(title2, img2, 100, q * 50);
 
}
}







void draw() {
  background(0);
  // Displays and scrolls each item
for (int i = 0; i < items.length; i++) {
for (int q = 0; q < items2.length; q++){
items[i].display();
items[i].scroll();
items2[q].display();
items2[q].scroll();
}
}
}

Replies(1)

Well, first problem, you define an arbitrary number, itemNumber2, and hope the data you get is big enough to loop on this number.
But actually, since the error is on index 0, it means the rss2.getChildren("channel/item/media:thumbnail") returns an empty array.
When getting data like this, it is better to check its validity and dimension.