首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >处理时出现空指针异常(ldrValues)

问处理时出现空指针异常(ldrValues)
EN

Stack Overflow用户
提问于 2019-04-20 22:05:09
回答 1查看 55关注 0票数 1

我的代码同时涉及Processing和Arduino。5种不同的光电池正在触发5种不同的声音。我的声音文件只在ldr值高于阈值时播放。

空指针异常在此行上突出显示

代码语言:javascript
复制
for (int i = 0; i < ldrValues.length; i++) {

我不确定应该更改代码的哪一部分才能运行它。

代码语言:javascript
复制
import processing.serial.*;
import processing.sound.*;

SoundFile[] soundFiles = new SoundFile[5];
Serial myPort;  // Create object from Serial class

int[] ldrValues;
int[] thresholds = {440, 490, 330, 260, 450};
int i = 0;
boolean[] states = {false, false, false, false, false};

void setup() {
  size(200, 200);


  println((Object[])Serial.list());

  String portName = Serial.list()[3];
  myPort = new Serial(this, portName, 9600);


  soundFiles[0] = new SoundFile(this, "1.mp3");
  soundFiles[1] = new SoundFile(this, "2.mp3");
  soundFiles[2] = new SoundFile(this, "3.mp3");
  soundFiles[3] = new SoundFile(this, "4.mp3");
  soundFiles[4] = new SoundFile(this, "5.mp3");
}

void draw()
{
  background(255);             

  //serial loop
  while (myPort.available() > 0) {
    String myString = myPort.readStringUntil(10);
    if (myString != null) {
      //println(myString);
      ldrValues = int(split(myString.trim(), ','));
      //println(ldrValues);
    }
  }

  for (int i = 0; i < ldrValues.length; i++) {
    println(states[i]);
    println(ldrValues[i]);
    if (ldrValues[i] > thresholds[i] && !states[i]) {
      println("sensor " + i + " is activated");
      soundFiles[i].play();
      states[i] = true;
    }
    if (ldrValues[i] < thresholds[i]) {
      println("sensor " + i + " is NOT activated");
      soundFiles[i].stop();
      states[i] = false;
    }
  }
}
EN

回答 1

Stack Overflow用户

发布于 2019-04-21 04:11:45

你的方法应该说是乐观的吗?:)

它总是假设有一条来自Serial的消息,总是以正确的方式格式化,所以它可以被解析,并且缓冲数据(不完整的字符串等)绝对没有问题。)

您可以做的最简单的事情是检查解析是否成功,否则ldrValues数组仍然为空:

代码语言:javascript
复制
void draw()
{
  background(255);             

  //serial loop
  while (myPort.available() > 0) {
    String myString = myPort.readStringUntil(10);
    if (myString != null) {
      //println(myString);
      ldrValues = int(split(myString.trim(), ','));
      //println(ldrValues);
    }
  }
  // double check parsing int values from the string was successfully as well, not just buffering the string  
  if(ldrValues != null){
    for (int i = 0; i < ldrValues.length; i++) {
      println(states[i]);
      println(ldrValues[i]);
      if (ldrValues[i] > thresholds[i] && !states[i]) {
        println("sensor " + i + " is activated");
        soundFiles[i].play();
        states[i] = true;
      }
      if (ldrValues[i] < thresholds[i]) {
        println("sensor " + i + " is NOT activated");
        soundFiles[i].stop();
        states[i] = false;
      }
    }
  }else{
    // print a helpful debugging message otherwise
    println("error parsing ldrValues from string: " + myString);
  }


}

(我不知道可以用int()来解析int[]:nice!)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55774280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档