gpt4 book ai didi

java - 从leapmotion传输到arduino返回错误: java. lang.ArrayIndexOutOfBoundsException:1

转载 作者:行者123 更新时间:2023-12-02 05:33:51 25 4
gpt4 key购买 nike

我正在通过处理将数据从 Leap-Motion 传输到 Arduino。但是,它总是返回错误“java.lang.ArrayIndexOutOfBoundsException:1”

这是生成错误的代码,但我只是不知道导致问题的原因。

import processing.serial.*;
Serial myPort;
import com.onformative.leap.LeapMotionP5;
import com.leapmotion.leap.Hand;

LeapMotionP5 leap;
float xhand;
float yhand;
float zhand;

void setup(){
frameRate(20);
size(500, 500, P2D);

myPort = new Serial(this, "COM7", 57600);
myPort.bufferUntil ('\n');
leap = new LeapMotionP5(this);
}

void draw(){
background(23,23,200);
ellipse(getHandX()+30, getHandZ()+320, 55, 55);

line( 200, 500, 200, 0);
line( 300, 500, 300, 0);
line( 0, 200, 500, 200);
line( 0, 300, 500, 300);

int handCt = 0;

for (Hand hand : leap.getHandList()) {
if (handCt == 0) {
PVector handPos = leap.getPosition(hand);
setHandPos( handPos.x, handPos.y, handPos.z );
}
handCt++;

int throttle = (int)map(getHandY(), height-100, -100, 0, 85);
throttle = constrain(throttle, 0, 85);
int pitch = (int)map(getHandZ(), -500, 1000, 171, 250);
pitch = constrain(pitch, 171, 250);
int yaw= (int)map(getHandX(), width-130, -width, 86, 170);
yaw = constrain(yaw, 86, 170);
myPort.write(yaw);
myPort.write(pitch);
myPort.write(throttle);
println(pitch);
}
}

最佳答案

当您尝试引用不存在的数组元素时,会抛出ArrayIndexOutOfBoundsException。这意味着指定的索引大于或等于数组的长度,因为数组中第一个元素的索引为 0(而不是 1)。

我希望它发生在这一行:

String portName = Serial.list()[1];

它尝试访问的数组一定只有一个元素,该元素位于索引 0 处。

关于java - 从leapmotion传输到arduino返回错误: java. lang.ArrayIndexOutOfBoundsException:1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25227793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com