我正在讀取arduino草圖上的串行數據,我上傳到teensy 2.0 /這在運行常規arduino時也會發生。然後,我分割字符串並將其轉換爲帶有Processing的int()函數的整數數組,以便能夠觸發聲音。 arduino草圖讀取電容式傳感器數據。它運行良好約10-15秒,然後開始拋出以下錯誤。處理從Arduino導致的Java PApplet錯誤的串行讀取
Exception in thread "Animation Thread" java.lang.NullPointerException
at processing.core.PApplet.parseInt(PApplet.java:9127)
at processing.core.PApplet.parseInt(PApplet.java:9113)
at NightGames_Tree.draw(NightGames_Tree.java:59)
at processing.core.PApplet.handleDraw(PApplet.java:2266)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:243)
at processing.core.PApplet.run(PApplet.java:2140)
at java.lang.Thread.run(Thread.java:695)
我的代碼如下: 在Arduino的:
#include <CapacitiveSensor.h>
CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2);
void setup()
{
cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example
Serial.begin(9600);
}
void loop()
{
long start = millis();
long total1 = cs_4_2.capacitiveSensor(30);
Serial.print(millis() - start); // check on performance in milliseconds
Serial.print("\t");
Serial.print(total1); // print sensor output 1
Serial.println("\t");
delay(100);
}
在處理:
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;
import processing.serial.*;
Minim minim;
AudioPlayer tree1;
AudioPlayer tree2;
Serial myPort;
String val;
void setup(){
String portName = Serial.list()[12]; // calls on the port which teensy is on
myPort = new Serial(this, portName, 9600);
println(Serial.list()); // prints list of ports that processing can access
//setup sound
minim = new Minim(this);
tree1 = minim.loadFile("tree1.aif");
}
void draw()
{
if (myPort.available() > 0) {
val = myPort.readStringUntil('\n');
int[] list = int(split(val, '\t')); // splits string into list based on tab after
int sum = 0;
int sum1 = 0;
// print(list.length);
sum = sum + list[0];
sum1 = sum1 + list[1];
print(sum);
print('\t');
print(sum1);
print('\t');
print('\n');
//print(val);
if (sum1 > 500) {
tree1.play(0);
} else if (sum1 <500){
}
}
}
當錯誤運行一段後顯示出來,處理突出顯示這一行的代碼
int[] list = int(split(val, '\t'));
在此先感謝您的任何幫助。我在Mac上運行這個。
您是否檢查過'split'的輸出? 'NullPointerException'意味着你試圖解析一個'null'參數。 –
謝謝哈立德,最好的辦法是什麼?我正在打印下面分組中的值。 – Alien
你可以使用'split',然後遍歷結果數組並檢查是否有'null',打印出來表明你找到了'null'值。 –