Java Lejos 抛出 nullpointerException

Java Lejos throws nullpointerException

您好 Whosebug 用户。这与 Java 而不是机器人本身有关。我试图做的是将传感器与运动方法分开,使代码易于阅读,但我 运行 遇到了一个问题。

Sensor.java

package sensors;

import lejos.hardware.sensor.EV3ColorSensor;
import lejos.hardware.sensor.EV3GyroSensor;
import lejos.hardware.sensor.EV3TouchSensor;
import lejos.hardware.sensor.EV3UltrasonicSensor;

public class Sensors {

    EV3TouchSensor touchSensor;
    EV3ColorSensor colorSensor;
    EV3GyroSensor gyroSensor;
    EV3UltrasonicSensor ultrasonicSensor;

    public Sensors(EV3TouchSensor t, EV3ColorSensor c, EV3GyroSensor g, EV3UltrasonicSensor u) {
        touchSensor = t;
        colorSensor = c;
        gyroSensor = g;
        ultrasonicSensor = u;
    }

    public int getColorSample(){
        int sample = colorSensor.getColorID();
        return sample;
    }

}

Movement.java

public class Movement {

    RegulatedMotor left;
    RegulatedMotor right;
    EV3TouchSensor touchSensor;
    EV3ColorSensor colorSensor;
    EV3GyroSensor gyroSensor;
    EV3UltrasonicSensor ultrasonicSensor;

    public Movement(RegulatedMotor l, RegulatedMotor r, EV3TouchSensor t,  EV3ColorSensor c, EV3GyroSensor g,
            EV3UltrasonicSensor u) {
        left = l;
        right = r;
        touchSensor = t;
        colorSensor = c;
        gyroSensor = g;
        ultrasonicSensor = u;
    }

    //initialize sensors

    Sensors sensors = new Sensors(touchSensor, colorSensor, gyroSensor, ultrasonicSensor);

    public void moveForward() {
        // get the color sample of the ground
        //int sample = colorSensor.getColorID();
        int sample = sensors.getColorSample();

        // while machine is on the ground color
        while (sample != 7) {

            // get new sample
            //sample = colorSensor.getColorID();
            sample = sensors.getColorSample();

            // move forward
            syncForward();
        }
        // if on black, stop motors.
        syncStop();
    }

别看其他方法,因为这些方法很流畅,但错误出现在第 30 行,它试图从传感器 class 获取样本。 我不知道,我也给出了注释掉的行,它们可以流畅地工作。 错误来自访问传感器 class,我想不出解决方案。

我会欠你的!

啊,我花了点时间才明白。

问题出在这一行: Sensors sensors = new Sensors(touchSensor, colorSensor, gyroSensor, ultrasonicSensor);

您已经在构造函数之后编写了此初始化,但是,sensors 将在 在构造函数中初始化您的字段之前 被赋值。这样,Sensors 的对象将仅使用空值创建,并且在调用 sensors.getColorSample() 时,Sensors class 中的行 int sample = colorSensor.getColorID(); 将抛出一个NullPointerException.

要修复它,请尝试像这样更改 Movement class:

public class Movement {

    RegulatedMotor left;
    RegulatedMotor right;
    EV3TouchSensor touchSensor;
    EV3ColorSensor colorSensor;
    EV3GyroSensor gyroSensor;
    EV3UltrasonicSensor ultrasonicSensor;

    // like in your code, sensors is still a field, but won't be initialized yet
    Sensors sensors;

    public Movement(RegulatedMotor l, RegulatedMotor r, EV3TouchSensor t,  EV3ColorSensor c, EV3GyroSensor g,
            EV3UltrasonicSensor u) {
        left = l;
        right = r;
        touchSensor = t;
        colorSensor = c;
        gyroSensor = g;
        ultrasonicSensor = u;

        // here is the initialization of sensors - at this point, the arguments shouldn't be null anymore (unless they are passed as null to the constructor)
        sensors = new Sensors(touchSensor, colorSensor, gyroSensor, ultrasonicSensor);
    }

    public void moveForward() {
        // this method should be okay, you don't need to change it

        // ...
    }

未来的一些提示:

  • 创建您的字段 private - 请参阅 Java 教程中的 Declaring Member Variables
  • 如果字段不允许空值(例如,当它可能导致 NullPointerException 时),请检查您的参数是否存在空值,如果存在非法值则抛出异常。
  • 尝试学习调试您的代码 - 您可能很快就发现 sensors 中的字段都是空的,然后您可以再次调试并了解为什么会这样。要进一步阅读 NullPointerExceptions,我建议 What is a Null Pointer Exception, and how do I fix it?

ClassMovement,对象EV3ColorSensor在构造函数中定义....class正在使用移动对象?

对传感器构造函数的引用为空。