We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › StringIndexOutOfBoundsException
Page Index Toggle Pages: 1
StringIndexOutOfBoundsException (Read 1120 times)
StringIndexOutOfBoundsException
May 19th, 2009, 1:46pm
 
Hello,
I have a device that will take in ASCII over the serial port and display it on a 16*2 character LCD (Sparkfun SerLCD). I made a program that would pull the titles of articles from Slashdot using romefeeder and display them in a ticker like format on the lcd. However, when I try to run it, it displays the first headline and then gives me a "StringIndexOutOfBoundsException" error. I can't figure out why this happens. Can anyone help me?

Code:
Quote:
import processing.serial.*;
import net.obxlabs.romefeeder.*;
import com.sun.syndication.feed.synd.*;
Serial lcd;
Feeder feed;
SyndEntry entry;
int i = 0;
int n = 0;

void setup()
{
  frameRate(5);
  feed = new Feeder();
  lcd = new Serial(this, Serial.list()[1], 9600);
  feed.load("http://rss.slashdot.org/Slashdot/slashdot");
  feed.sort(Feeder.PUBLISHED_DATE);
  feed.setUpdateInterval(500);
  feed.verbose = true;
  feed.setProxy("http://www.brunonadeau.com/romefeeder/examples/SimpleFeed/applet/proxy.php");
  feed.startUpdate();
  entry = feed.next();
}

void draw()
{
  lcd.write(char(254)); //Stuff to put the "cursor" at the beginning of the LCD
  lcd.write(char(128));
  lcd.write("S l a s h d o t ");
  if (entry != null)
  {
    n = entry.getTitle().length();
  }
  if(i < n+16 && entry != null)
  {
    i++;
    lcd.write(entry.getTitle().substring(0+i, 16+i));
  } else {
    i = 0;
    entry = feed.next();
  }
}

/*
The output of this program looks like this:
 ______________
|S L A S H D O T |
|Tickerartcltitle    |

The titles scroll one character at a time.
**/


Re: StringIndexOutOfBoundsException
Reply #1 - May 19th, 2009, 2:37pm
 
Probably coming from
lcd.write(entry.getTitle().substring(0+i, 16+i));

Is (16+i) the highest possible or would it be (15+i)?

Highest index of an array is (length-1) since it starts at zero.
Re: StringIndexOutOfBoundsException
Reply #2 - May 19th, 2009, 3:16pm
 
I tried changing the numbers around but I never got any results.
Thanks for the suggestion tho
Re: StringIndexOutOfBoundsException
Reply #3 - May 19th, 2009, 11:27pm
 
Faradays_Cage wrote on May 19th, 2009, 1:46pm:
  if(i < n+16 && entry != null)
  {
    i++;
    lcd.write(entry.getTitle().substring(0+i, 16+i));
  } else {

I would increment i after the substring call, not before, unless you want to skip the first char.
Page Index Toggle Pages: 1