Advice on using try() and catch() functions.
in
Programming Questions
•
1 year ago
Hello,
I'm using Processing to read sensor values from an Arduino via com port. Everything seems to work fine for a couple of hours or so, but eventually one of the last values in the array
String[] readList = splitTokens(getRawArduino);
comes back null and causes a java.lang.ArrayIndexOutOfBoundsException at 11, 12, 13 or 14. Usually it happens at 11 though. The following variables are the source of the null values, and are all coming from the SHT15 temp/humidity sensor:
- temp_c_indoor = float(readList[11]);
- temp_f_indoor = float(readList[12]);
- dewpoint_f_indoor = float(readList[13]);
- relative_humidity_indoor = float(readList[14]);
Once in a while the sensor will return 0.0000 causing the null in processing. I'm trying to implement a try() and catch() function to deal with this, but I'm not understanding how it's set up and I'm hoping somebody can point out what I'm doing wrong.
- void draw(){
- if (lastQueryTime + 600000 > millis() && lastQueryTime < 100){
- getXML();
- doQueryExecuteDB();
- lastQueryTime = millis();
- }
- if (arduinoCom + 120000 < millis()){
- try{
- nullException = readList.readArduino();
- }
- catch (ArrayIndexOutOfBoundsException) {
- nullException = null;
- }
- if (nullException == null){
- noLoop();
- }
- else{
- readArduino();
- }
- writeArduino();
- doWriteArduinoDB();
- arduinoCom = millis();
- }
- if (lastQueryTime + 600000 < millis()) {
- getXML();
- doQueryExecuteDB();
- lastQueryTime = millis();
- }
- }
- void readArduino(){
- //get the sensor and actuator data from the serial port
- if (comPort4.available() > 0)
- {
- getRawArduino = comPort4.readString();
- String[] readList = splitTokens(getRawArduino);
- //convert strings to floats
- intLightSens1 = int(readList[0]); // intLightSens1
- intLightSens2 = int(readList[1]); // intLightSens2
- intLightSens3 = int(readList[2]); // intLightSens3
- avgInteriorLight = int(readList[3]); // avgInteriorLight
- extLightSens1 = int(readList[4]); // extLightSens1
- extLightSens2 = int(readList[5]); // extLightSens2
- extLightSens3 = int(readList[6]); // extLightSens3
- extLightSens4 = int(readList[7]); // extLightSens4
- extLightSens5 = int(readList[8]); // extLightSens5
- solarPosition = int(readList[9]); // solarPosition
- actuatorPercent = int(readList[10]); // actuatorPercent
- temp_c_indoor = float(readList[11]); // temp_c_indoor
- temp_f_indoor = float(readList[12]); // temp_f_indoor
- dewpoint_f_indoor = float(readList[13]); // dewpoint_f_indoor
- relative_humidity_indoor = float(readList[14]); // relative_humidity_indoor
- }
- else{
- println ("Not connected to Arduino");
- }
- }
1