gpt4 book ai didi

java - Lejos EV3 "Null Pointer Exception",但我不知道如何修复它

转载 作者:太空宇宙 更新时间:2023-11-04 09:51:39 27 4
gpt4 key购买 nike

我正在为我的机器人俱乐部设置 Java 和 lejos,并且正在创建一些基本的驱动方法,但是当我将其组织到方法中时,它告诉我出现了空指针异常。问题是我不知道它在哪里或如何修复它。

<小时/>
import lejos.hardware.motor.*;
import lejos.hardware.port.*;

public class HardwareMappings {

EV3LargeRegulatedMotor leftDrive = null;
EV3LargeRegulatedMotor rightDrive = null;


public void init(HardwareMappings ahwMap) {
leftDrive = new EV3LargeRegulatedMotor(MotorPort.B);
rightDrive = new EV3LargeRegulatedMotor(MotorPort.C);

}

}
<小时/>
public class DrivetrainMethods {

HardwareMappings robot = new HardwareMappings();

public void TimedDrive(int direction, int power, double time) throws InterruptedException {
robot.leftDrive.setSpeed((power * 60) - direction);
robot.rightDrive.setSpeed((power * 60) + direction);
Thread.sleep((long) (time * 60));
robot.leftDrive.stop(true);
robot.rightDrive.stop(true);
}

public void TankDrive (int leftPower, int rightPower, double time) throws InterruptedException {
robot.leftDrive.setSpeed(leftPower);
robot.rightDrive.setSpeed(rightPower);
Thread.sleep((long) (time * 60));
robot.leftDrive.stop(true);
robot.rightDrive.stop(true);
}
}
<小时/>
public class Test {

public static void main (String[] args) throws InterruptedException{

DrivetrainMethods drivetrain = new DrivetrainMethods();

drivetrain.TimedDrive(0, 50, 1);
drivetrain.TankDrive(-50, -50, 1);
}
}
<小时/>

请帮忙!
谢谢

附: (每段代码应该是一个单独的文件。)

最佳答案

请更改:

public void init(HardwareMappings ahwMap) {//this code will be invoked on:
// someHardwareMappings.init(otherHardwareMappings);
leftDrive = new EV3LargeRegulatedMotor(MotorPort.B);
rightDrive = new EV3LargeRegulatedMotor(MotorPort.C);
}

至:

public HardwareMappings() { // this code will be invoked on:
// new HardwareMappings();)
leftDrive = new EV3LargeRegulatedMotor(MotorPort.B);
rightDrive = new EV3LargeRegulatedMotor(MotorPort.C);
}

这将修复您的 NPE,这就是 的方式(不是 python!;)构造函数看起来像。

关于java - Lejos EV3 "Null Pointer Exception",但我不知道如何修复它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54660434/

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