# \[SOLVED\] ArrayIndexOutOfBoundsException: 1

**URL:** <https://discourse.processing.org/t/solved-arrayindexoutofboundsexception-1/2288>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [August 1, 2018, 10:39pm UTC](https://discourse.processing.org/t/solved-arrayindexoutofboundsexception-1/2288 "2018-08-01T22:39:56Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![racs](https://avatars.discourse-cdn.com/v4/letter/r/958977/32.png) [@racs](https://discourse.processing.org/u/racs)\
**Post date:** [August 1, 2018, 10:39pm UTC](https://discourse.processing.org/t/solved-arrayindexoutofboundsexception-1/2288/1 "2018-08-01T22:39:56Z")

</div>

Using an arduino Mega to collect temperature and humidity data then using Processing to display data on meters. Everything works fine for a few seconds then I receive this error: ArrayIndexOutOfBoundsException: 1. Not sure why?

**Arduino Code:**

```auto
#include <Wire.h>
#include "Wire.h"
#include "Adafruit_HTU21DF.h"
#include <SPI.h>
#include <SoftwareSerial.h>
extern "C" { 
#include "utility/twi.h" // from Wire library, so we can do bus scanning
}

uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);

uint8_t type;
#define TCAADDR 0x70 // mux 1 #define TCAADDR 0x70 // mux 1 
#define TCAADDR2 0x71 // mux 2 #define TCAADDR 0x71 // mux 2 
#define TCAADDR3 0x72 // mux 3 #define TCAADDR 0x72 // mux 3
#define TCAADDR4 0x73 // mux 4 #define TCAADDR 0x73 // mux 4

Adafruit_HTU21DF htu = Adafruit_HTU21DF();
// array of temperature designated by [] when calling call from 0-9 for an array of size 10
float temperature[3]= {-1, -1, -1};
float humidity[3]= {-1, -1, -1};
float humiditymax[10]={60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0};
float humiditymin[10]={24.0, 24.0, 24.0, 24.0, 24.0, 24.0, 24.0, 24.0, 24.0, 24.0};
//temperature max is 100F and min is 60F
float temperaturemax[10]={37.8, 37.8, 37.8, 37.8, 37.8, 37.8, 37.8, 37.8, 37.8, 37.8};
float temperaturemin[10]={15.55, 15.55, 15.55, 15.55, 15.55, 15.55, 15.55, 15.55, 15.55, 15.55};
//temp below 60 above 100

void setup() {

  // put your setup code here, to run once:
            Wire.begin(); // Initiate the Wire library
            Serial.begin(9600);
            tcaselect(7);
            htu.begin();
}

void loop() {
  // put your main code here, to run repeatedly:
  

//Serial.println("in loop");

            ///////////////send data from sensors to the lath house , main room, green house 1 and 2 ////////////
            // Ex. tcaselect(1) is option 1 temperature/humidity reading, tcaselect(2) is option 2 temperature/humidity reading..etc
            //total 10 options/readings, mux1 reads from sensors 0-3, mux2 reads from sensors 4-7, mux3 reads from sensors 8 & 9
            //mux1
            tcaselect(1);
            temperature[0] = htu.readTemperature();
            humidity[0] = htu.readHumidity();
            
            tcaselect(2);
            temperature[1] = htu.readTemperature();
            humidity[1] = htu.readHumidity();
            
            tcaselect(6);
            temperature[2] = htu.readTemperature();
            humidity[2] = htu.readHumidity();

    Serial.print((int)temperature[0]);
    Serial.print(",");
  Serial.println((int)temperature[1]);

  
  delay(2500);
            
}

void tcaselect(uint8_t i) {   
      // set the mux we want to the correct loacation// reading from mux1, disable other muxes
      if (i > 7) return;
          Wire.beginTransmission(TCAADDR);
          Wire.write(1 << i);
          Wire.endTransmission(); 
             //disable all other muxes
             Wire.beginTransmission(TCAADDR2);
             Wire.write(0); // no channel selected
             Wire.endTransmission();
             Wire.beginTransmission(TCAADDR3);
             Wire.write(0); // no channel selected
             Wire.endTransmission();
             Wire.beginTransmission(TCAADDR4);
             Wire.write(0); // no channel selected
             Wire.endTransmission();
}

```

**Processing Code:**

```auto
/ ***********************************
 First upload the Arduino code to the Arduino and then run this code.
 Make sure that the Port number and baudrate are the same.
*********************************** /
import meter.*;
import processing.serial.*;

Serial port;
String[] list;

Meter m, m2;

void setup() {
  size(950, 400);
  background(0);
  
  port = new Serial(this, "/dev/ttyACM0", 9600);
 

  fill(120, 50, 0);
  m = new Meter(this, 25, 100);
  // Adjust font color of meter value  
  m.setTitleFontSize(20);
  m.setTitleFontName("Arial bold");
  m.setTitle("Temperature (C)");
  m.setDisplayDigitalMeterValue(true);
  
  // Meter Scale
  String[] scaleLabelsT = {"0", "10", "20", "30", "40", "50", "60", "70", "80"};
  m.setScaleLabels(scaleLabelsT);
  m.setScaleFontSize(18);
  m.setScaleFontName("Times New Roman bold");
  m.setScaleFontColor(color(200, 30, 70));
  
  m.setArcColor(color(141, 113, 178));
  m.setArcThickness(10);
  m.setMaxScaleValue(80);
  
  m.setNeedleThickness(3);
  
  m.setMinInputSignal(0);
  m.setMaxInputSignal(80);

  // A second meter for reference
  int mx = m.getMeterX();
  int my = m.getMeterY();
  int mw = m.getMeterWidth();
  
  m2 = new Meter(this, mx + mw + 20, my);
  m2.setTitleFontSize(20);
  m2.setTitleFontName("Arial bold");
  m2.setTitle("Humidity (%)");
  m2.setDisplayDigitalMeterValue(true);
  
  String[] scaleLabelsH = {"0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"};
  m2.setScaleLabels(scaleLabelsH);
  m2.setScaleFontSize(18);
  m2.setScaleFontName("Times New Roman bold");
  m2.setScaleFontColor(color(200, 30, 70));
  
  m2.setArcColor(color(141, 113, 178));
  m2.setArcThickness(10);
  m2.setMaxScaleValue(100);
  
  m2.setNeedleThickness(3);
  
  m2.setMinInputSignal(0);
  m2.setMaxInputSignal(100);
  
}

public void draw() {
  
  textSize(30);
  fill(0, 255, 0);
  text("Temperature and Humidity", 270, 50);
  
  if (port.available() > 0) {
    String val = port.readString();
    list = split(val, ',');
    float temp = float(list[0]);
    float hum = float(list[1]);
    
    println("Temperature: " + temp + " C " + "Humidity: " + hum + " %");
    
    m.updateMeter(int(temp));
    m2.updateMeter(int(hum));
  delay(2500);
  }
}

```

 ![Screenshot%202018-08-01%2015%3A41%3A46](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/1X/12e915a4a19cea4a91da88f431be375c1015d211.png)

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [August 1, 2018, 11:00pm UTC](https://discourse.processing.org/t/solved-arrayindexoutofboundsexception-1/2288/2 "2018-08-01T23:00:27Z")

</div>

What confuses me is that, from what you’ve showed us, it splits “Val” fine, until suddenly it doesn’t.

I’m not too familiar with Arduino stuff -

> [@racs](#):
>
> String val = port.readString();

How do you know this line will, for certain, spit out two floats every draw() cycle?

---

<div class="post-metadata">

**Author:** ![shawnlau](https://avatars.discourse-cdn.com/v4/letter/s/5fc32e/32.png) [@shawnlau](https://discourse.processing.org/u/shawnlau)\
**Post date:** [August 1, 2018, 11:10pm UTC](https://discourse.processing.org/t/solved-arrayindexoutofboundsexception-1/2288/3 "2018-08-01T23:10:43Z")

</div>

You could put some debug code in the loop:

```auto
if( list.length < 2)
     println(val);

```

Or if the length is less than 2, reread the port.

---

<div class="post-metadata">

**Author:** ![racs](https://avatars.discourse-cdn.com/v4/letter/r/958977/32.png) [@racs](https://discourse.processing.org/u/racs)\
**Post date:** [August 1, 2018, 11:20pm UTC](https://discourse.processing.org/t/solved-arrayindexoutofboundsexception-1/2288/4 "2018-08-01T23:20:34Z")

</div>

This is code from a tutorial on youtube [https://www.youtube.com/watch?v=YgjsWvFRpSE&feature=youtu.be](https://www.youtube.com/watch?v=YgjsWvFRpSE&feature=youtu.be) that I’m using and in the tutorial it works fine. I think it should spit out two values because of the line: list = split(val, ‘,’); it should split the data according to the comma it reads.

My theory is that the serial monitor isn’t printing out values consistently. For example it should be:

29, 29  
29, 29  
30, 29

BUT maybe its printing out like:

29,29  
29,  
30, 29

which it might crash/freeze? I’m not sure honestly

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [August 2, 2018, 12:19am UTC](https://discourse.processing.org/t/solved-arrayindexoutofboundsexception-1/2288/5 "2018-08-02T00:19:34Z")

</div>

> [@racs](#):
>
> My theory is that the serial monitor isn’t printing out values consistently

This sounds the most likely. As @shawnlau suggests, I would test if length \> 1before reading from the list.

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [August 2, 2018, 3:23am UTC](https://discourse.processing.org/t/solved-arrayindexoutofboundsexception-1/2288/6 "2018-08-02T03:23:20Z")

</div>

> [@racs](#):
>
> Serial.print((int)temperature[0]);  
> Serial.print(“,”);  
> Serial.println((int)temperature[1]);

I suggest you check previous code related to Arduino. For instance: [Processing with Arduino - void serialEvent - Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item_5)

Notice your data stream ends with a new character so you could use:

`port.bufferUntil('\n');`

After that you need to:

1. Check the data you got is not null
2. Proceed to splitting (you are doing that above)
3. Before you access your data, you need to make sure you have an array of size 2. Doing this type of check it is to ensure you dont run into null point exceptions (NPEs) or similar situations
4. Apply `trim()`, just in case, to ensure you remove unwanted characters
5. Extract values

```java
if (port.available() > 0) {
    String val = port.readStringUntil(`\n`);
    if(val!=null){
       list = split(val, ',');
       if(list.length==2){
           float temp = float(trim(list[0]));
         float hum = float(trim(list[1]));
        }
    }
}

```

Kf

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 2, 2018, 10:33am UTC](https://discourse.processing.org/t/solved-arrayindexoutofboundsexception-1/2288/7 "2018-08-02T10:33:08Z")

</div>

For multiple data transmission, I recommend you to read this post below: 🔌

- [Forum.Processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item\_1](http://Forum.Processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item_1)

1. As you can see, on the Arduino side, separate the data w/ `\t` using **print()** + **write()**.
2. And then send the last data w/ **println()**.
3. On the Processing side, inside **serialEvent()**, use Serial::**readString()** + PApplet.**splitTokens()** in order to get all the data as a String[] array.
4. Of course, you’re still gonna need to convert each data in that received array to their corresponding datatype if that data isn’t a String.
5. And don’t forget to use `bufferUntil(ENTER)` after instantiating the class Serial. ⚠

---

<div class="post-metadata">

**Author:** ![erwrow](https://avatars.discourse-cdn.com/v4/letter/e/b782af/32.png) [@erwrow](https://discourse.processing.org/u/erwrow)\
**Post date:** [August 2, 2018, 11:19am UTC](https://discourse.processing.org/t/solved-arrayindexoutofboundsexception-1/2288/8 "2018-08-02T11:19:01Z")

</div>

i would enclose this part:

```auto
if (port.available() > 0) {
    String val = port.readString();
    list = split(val, ',');
    float temp = float(list[0]);
    float hum = float(list[1]);
    
    println("Temperature: " + temp + " C " + "Humidity: " + hum + " %");
    
    m.updateMeter(int(temp));
    m2.updateMeter(int(hum));
  delay(2500);
  }

```

in a try function, probably your sensor fails to recover tmperatura or humidity and don’t send anithing, another thing to do is to verify (in the arduino code) the integrity of the data before sending it.

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [August 2, 2018, 2:44pm UTC](https://discourse.processing.org/t/solved-arrayindexoutofboundsexception-1/2288/9 "2018-08-02T14:44:16Z")

</div>

@erwrow I wouldn’t recommend using the delay within the Processing code. The serial event is a call back function. It will return as soon as some data is present in its buffer. Checking for data integrity could be done in the arduino or in Processing. I recommend to do it in the Processing side as it is easier to debug. I see the arduino more of a hub between the sensors and the user: it relays the data without many modifications. This is my prefered concept but it is up to the designer of the code to do what he thinks is most convenient for his project.

Kf

---

<div class="post-metadata">

**Author:** ![erwrow](https://avatars.discourse-cdn.com/v4/letter/e/b782af/32.png) [@erwrow](https://discourse.processing.org/u/erwrow)\
**Post date:** [August 2, 2018, 3:01pm UTC](https://discourse.processing.org/t/solved-arrayindexoutofboundsexception-1/2288/10 "2018-08-02T15:01:29Z")

</div>

i agreed, i don’t use delays either, i just copyed that part of the code. About data integrity i was refering to “see” if the sensor returns NaN or a correct value, but yes, you’re right!.

---

<div class="post-metadata">

**Author:** ![racs](https://avatars.discourse-cdn.com/v4/letter/r/958977/32.png) [@racs](https://discourse.processing.org/u/racs)\
**Post date:** [August 2, 2018, 10:03pm UTC](https://discourse.processing.org/t/solved-arrayindexoutofboundsexception-1/2288/11 "2018-08-02T22:03:19Z")

</div>

this seems to have solved the issue and it was a simple fix. Thanks!

---

<div class="post-metadata">

**Author:** ![racs](https://avatars.discourse-cdn.com/v4/letter/r/958977/32.png) [@racs](https://discourse.processing.org/u/racs)\
**Post date:** [August 2, 2018, 10:04pm UTC](https://discourse.processing.org/t/solved-arrayindexoutofboundsexception-1/2288/12 "2018-08-02T22:04:27Z")

</div>

Thank you for your suggestion. Debugging helped!
