# serialEvent out of bounds error

**URL:** https://discourse.processing.org/t/serialevent-out-of-bounds-error/11272
**Category:** Electronics (Arduino, etc.)
**Created:** [May 15, 2019, 1:44pm UTC](https://discourse.processing.org/t/serialevent-out-of-bounds-error/11272 "2019-05-15T13:44:45Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![alan856](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@alan856](https://discourse.processing.org/u/alan856)
#### Post date: [May 15, 2019, 1:44pm UTC](https://discourse.processing.org/t/serialevent-out-of-bounds-error/11272/1 "2019-05-15T13:44:45Z")

</div>

Below is the function causing the exception, followed by the error message. This HAD been working - not sure what I’ve done differently to ‘break’ it. Any ideas where to start looking?

=A.

```auto
void serialEvent(Serial comPort) {
  try {
    String comBuf = comPort.readStringUntil('\n');
    if (comBuf != null) {
      comBuf = trim(comBuf);
      if (comBuf.equals("A")) {
        comPort.clear();
        isConnected = true;
        wasConnected = true;
        ardup = true;
        comPort.write("U"); // let Ard know we are up
      } else {
        ardCmd= comBuf.charAt(0);
        comPort.clear();
        ardup = false;
        evalData();
      }
    } else comBuf = ""; // keep it clean?
  }
  catch(RuntimeException e) {
    e.printStackTrace();
  }
}

```

And here is the error message produced by catch:

```auto
java.lang.StringIndexOutOfBoundsException: String index out of range: 0
	at java.lang.String.charAt(String.java:658)
	at Radar_V11.serialEvent(Radar_V11.java:210)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at processing.serial.Serial.serialEvent(Unknown Source)
	at jssc.SerialPort$EventThread.run(SerialPort.java:1112)

```

---

<div class="post-metadata">

### Author: ![humayung](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/humayung/32/6089_2.png) [@humayung](https://discourse.processing.org/u/humayung)
#### Post date: [May 16, 2019, 8:08am UTC](https://discourse.processing.org/t/serialevent-out-of-bounds-error/11272/2 "2019-05-16T08:08:12Z")

</div>

I suspected the error caused by `trim()`.  
I copied the `trim()` description from [trim() / Reference / Processing.org](https://processing.org/reference/trim_.html)

> **Removes whitespace characters from the beginning and end of a String. In addition to standard whitespace characters such as space, carriage return, and tab, this function also removes the Unicode “nbsp” character.**

The error occurred on line (i think)

`ardCmd= comBuf.charAt(0);`

Probably, comBuf **only** contain whitespace characters, and then you trim it. so the length will be **zero**. This will cause  
`StringIndexOutOfBoundsException`  
error when you execute  
`ardCmd= comBuf.charAt(0);`  
because no char at position 0

Test case

```auto
String a = " "; // SPACES
char b;
void setup() {
  try {
    a = trim(a);
    b = a.charAt(0);
  }
  catch(RuntimeException e) {
    e.printStackTrace();
  }
}

```

Further read: [https://developer.android.com/reference/java/lang/StringIndexOutOfBoundsException](https://developer.android.com/reference/java/lang/StringIndexOutOfBoundsException)

---

<div class="post-metadata">

### Author: ![alan856](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@alan856](https://discourse.processing.org/u/alan856)
#### Post date: [May 16, 2019, 10:28am UTC](https://discourse.processing.org/t/serialevent-out-of-bounds-error/11272/3 "2019-05-16T10:28:36Z")

</div>

Sorry - it was just the port was set to the wrong baud rate. Once corrected the code worked as it always had.
