【问题标题】:Null pointer exception on Processing (ldrValues)处理上的空指针异常 (ldrValues)
【发布时间】:2019-04-20 14:05:08
【问题描述】:

我的代码涉及 Processing 和 Arduino。 5 个不同的光电管触发 5 种不同的声音。我的声音文件仅在 ldrvalue 高于阈值时播放。

空指针异常在这一行突出显示

for (int i = 0; i < ldrValues.length; i++) {

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

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;
    }
  }
}

【问题讨论】:

    标签: nullpointerexception arduino processing


    【解决方案1】:

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

    总是假设有来自 Serial 的消息,总是以正确的方式格式化以便可以对其进行解析,并且缓冲数据的问题绝对是 0(不完整的字符串等))

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

    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[]:太好了!)

    【讨论】:

      猜你喜欢
      • 2021-11-08
      • 2016-09-11
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多