This question already has answers here:
What is a NullPointerException, and how do I fix it?
                                
                                    (12个答案)
                                
                        
                4年前关闭。
            
        

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

传感器.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;
    }

}


运动.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 = new Sensors(touchSensor, colorSensor, gyroSensor, ultrasonicSensor);

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

要解决此问题,请尝试更改Movement类,如下所示:

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?

10-04 23:47