JavaLejos抛出nullpointerException

JavaLejos抛出nullpointerException,java,nullpointerexception,mindstorms,Java,Nullpointerexception,Mindstorms,你好,StackOverflow用户。这与Java有关,而不是机器人本身。我试图做的是将传感器与运动方法分离,使代码易于阅读,但我遇到了一个问题 Sensor.java package sensors; import lejos.hardware.sensor.EV3ColorSensor; import lejos.hardware.sensor.EV3GyroSensor; import lejos.hardware.sensor.EV3TouchSensor; import lejos.

你好,StackOverflow用户。这与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行,它试图从sensors类获取样本。 我不知道,我也给出了注释掉的行,它们工作得很流利。 错误来自访问sensors类,我无法想出解决方案


我欠你的债

啊,我花了一段时间才明白

问题在于这一行:
传感器=新传感器(触摸传感器、彩色传感器、陀螺传感器、超声波传感器)

您已经在构造函数之后编写了此初始化,但在初始化构造函数中的字段之前,
传感器将被分配。这样,将仅使用空值创建
Sensors
的对象,并且在调用
Sensors.getColorSample()
时,行
int-sample=colorSensor.getColorID()传感器
类中的code>将引发NullPointerException

要修复它,请尝试更改
移动
类,如下所示:

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

        // ...
    }
未来的几点建议:

  • 将字段设置为私有的
——请参见Java教程中的
  • 如果字段不允许空值(例如,当它可能导致NullPointerException时),请检查参数中的空值,如果存在非法值,则引发异常
  • 试着学会调试你的代码-你可以很快看到你的
    传感器中的字段都是空的,然后你可以再次调试,看看为什么会这样。对于有关NullPointerException的进一步阅读,我推荐

  • 移动,对象
    EV3ColorSensor
    在构造函数中定义。。。。哪个类正在使用移动对象


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

    请在代码段中标记第30行,然后发布stacktrace?除了初始化之外,还有其他分配给传感器的吗?第30行是
    sample=sensors.getColorSample()它引用了“public int getColorSample(){`
    int sample=colorSensor.getColorID();
    返回样本;
    需要一些时间来写下stacktrace@EgertAia你不必写下来,你可以复制;)但我想我已经修好了,看看我的答案。谢谢@stuXnet。你真的又救了我一天。告诉我有没有可能从movement类的构造函数中删除传感器,然后离开e改为sensors.java的所有内容?你所说的private是什么意思?在main中还是仅仅在movement和sensors中?ad“private”:我在回答中添加了一个指向java教程的链接。而且你可以重构你的Movements构造函数,使它只接受两个motor参数和你的(先前初始化的)sensor对象。