FTC Android Studio用户代码引发了一个未捕获的异常:NullPointerException

时间:2015-12-10 04:50:33

标签: java android android-studio nullpointerexception

我在我学校的高中FIRST机器人团队,5108团队。我一直致力于一个可以适应大多数机器人的自主程序,并且非常容易改变。基本上所有用户必须输入的是机器人的起点,基于覆盖在比赛场地上的坐标平面。

但是当我尝试运行它时,我仍然遇到此错误。我已多次查看代码,没有错误或警告,我还使用了android studio提供的代码检查,并改变了它所推荐的一切,没有运气。如果有任何关于该计划的问题只是问,我是新的,所以有些可能不是那么好。

package com.qualcomm.ftcrobotcontroller.opmodes;

import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.hardware.HiTechnicNxtCompassSensor;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorController;

import java.util.ArrayList;


/**
 * Created by Tyler Eads on 12/7/15.
 */

@SuppressWarnings("deprecation")
public class SmartAutonomous  extends OpMode {

    private double distance;
    private double angle;
    private double currentx;
    private double currenty;
    private double targetx;
    private double targety;





    private HiTechnicNxtCompassSensor compassSensor;

    private final double compassvalue = compassSensor.getDirection();


    private final int countsperrotation = 1440;


    // INSERT ROBOT INFORMATION HERE...

    private final double wheeldiameter = 3.0;      //Insert Wheel Diameter Here
    private final double circumference = wheeldiameter * Math.PI;
    private final double gearratio = 1;         //write as fraction

    //END ROBOT INFORMATION


    private void distance(double currentx, double currenty, double targetx, double targety) {

        distance = Math.sqrt(((currentx - targetx) * (currentx - targetx)) + ((currenty - targety) * (currenty - targety)));

    }

    private void angle(double currentx, double currenty, double targetx, double targety) {

        angle = Math.toDegrees(Math.atan((targety-currenty)/(targetx-currentx)));

    }

    private final double wheelrotations = distance / circumference;
    private final double motorrotations = wheelrotations * gearratio;
    private final double encodercounts = motorrotations * countsperrotation;




    public enum Facing {North, NorthEast, East, SouthEast, South, SouthWest, West, NorthWest}
    private Facing facing;

    public enum RelativePosition {North, NorthEast, East, SouthEast, South, SouthWest, West, NorthWest}
    private RelativePosition relativeposition;





    private void facing() {
        if (260 <= compassvalue && compassvalue <= 264) {
            facing = Facing.North;
        } else if (264 < compassvalue && compassvalue < 348) {
            facing = Facing.NorthEast;
        } else if (348 <= compassvalue && compassvalue <= 352) {
            facing = Facing.East;
        } else if (352 < compassvalue && compassvalue < 88) {
            facing = Facing.SouthEast;
        } else if (88 <= compassvalue && compassvalue <= 92) {
            facing = Facing.South;
        } else if (92 < compassvalue && compassvalue < 178) {
            facing = Facing.SouthWest;
        } else if (178 <= compassvalue && compassvalue <= 182) {
            facing = Facing.West;
        } else if (182 < compassvalue && compassvalue < 260) {
            facing = Facing.NorthWest;
        }

    }

    private void pointrelative() {
        switch (facing) {
            case North:

                if (currenty < targety && currentx == targetx) {

                    relativeposition = RelativePosition.North;

                } else if (currenty < targety && currentx < targetx) {
                    relativeposition = RelativePosition.NorthEast;
                } else if (currenty == targety && currentx < targetx) {
                    relativeposition = RelativePosition.East;
                } else if (currenty < targety && currentx < targetx) {
                    relativeposition = RelativePosition.SouthEast;
                } else if (currenty > targety && currentx == targetx) {
                    relativeposition = RelativePosition.South;
                } else if (currenty > targety && currentx > targetx) {
                    relativeposition = RelativePosition.SouthWest;
                } else if (currenty == targety && currentx > targetx) {
                    relativeposition = RelativePosition.West;
                } else if (currenty < targety && currentx > targetx) {
                    relativeposition = RelativePosition.NorthWest;
                }

                break;

            case NorthEast:

                if (currenty < targety && currentx == targetx) {

                    relativeposition = RelativePosition.NorthWest;
                } else if (currenty < targety && currentx < targetx) {
                    relativeposition = RelativePosition.North;
                } else if (currenty == targety && currentx < targetx) {
                    relativeposition = RelativePosition.NorthEast;
                } else if (currenty < targety && currentx < targetx) {
                    relativeposition = RelativePosition.East;
                } else if (currenty > targety && currentx == targetx) {
                    relativeposition = RelativePosition.SouthEast;
                } else if (currenty > targety && currentx > targetx) {
                    relativeposition = RelativePosition.South;
                } else if (currenty == targety && currentx > targetx) {
                    relativeposition = RelativePosition.SouthWest;
                } else if (currenty < targety && currentx > targetx) {
                    relativeposition = RelativePosition.West;
                }

                break;

            case East:

                if (currenty < targety && currentx == targetx) {

                    relativeposition = RelativePosition.West;
                } else if (currenty < targety && currentx < targetx) {
                    relativeposition = RelativePosition.NorthWest;
                } else if (currenty == targety && currentx < targetx) {
                    relativeposition = RelativePosition.North;
                } else if (currenty < targety && currentx < targetx) {
                    relativeposition = RelativePosition.NorthEast;
                } else if (currenty > targety && currentx == targetx) {
                    relativeposition = RelativePosition.East;
                } else if (currenty > targety && currentx > targetx) {
                    relativeposition = RelativePosition.SouthEast;
                } else if (currenty == targety && currentx > targetx) {
                    relativeposition = RelativePosition.South;
                } else if (currenty < targety && currentx > targetx) {
                    relativeposition = RelativePosition.SouthWest;
                }

                break;

            case SouthEast:

                if (currenty < targety && currentx == targetx) {

                    relativeposition = RelativePosition.SouthWest;
                } else if (currenty < targety && currentx < targetx) {
                    relativeposition = RelativePosition.West;
                } else if (currenty == targety && currentx < targetx) {
                    relativeposition = RelativePosition.NorthWest;
                } else if (currenty < targety && currentx < targetx) {
                    relativeposition = RelativePosition.North;
                } else if (currenty > targety && currentx == targetx) {
                    relativeposition = RelativePosition.NorthEast;
                } else if (currenty > targety && currentx > targetx) {
                    relativeposition = RelativePosition.East;
                } else if (currenty == targety && currentx > targetx) {
                    relativeposition = RelativePosition.SouthEast;
                } else if (currenty < targety && currentx > targetx) {
                    relativeposition = RelativePosition.South;
                }

                break;

            case South:

                if (currenty < targety && currentx == targetx) {

                    relativeposition = RelativePosition.South;
                } else if (currenty < targety && currentx < targetx) {
                    relativeposition = RelativePosition.SouthWest;
                } else if (currenty == targety && currentx < targetx) {
                    relativeposition = RelativePosition.West;
                } else if (currenty < targety && currentx < targetx) {
                    relativeposition = RelativePosition.NorthWest;
                } else if (currenty > targety && currentx == targetx) {
                    relativeposition = RelativePosition.North;
                } else if (currenty > targety && currentx > targetx) {
                    relativeposition = RelativePosition.NorthEast;
                } else if (currenty == targety && currentx > targetx) {
                    relativeposition = RelativePosition.East;
                } else if (currenty < targety && currentx > targetx) {
                    relativeposition = RelativePosition.SouthEast;
                }

                break;

            case SouthWest:

                if (currenty < targety && currentx == targetx) {

                    relativeposition = RelativePosition.SouthEast;
                } else if (currenty < targety && currentx < targetx) {
                    relativeposition = RelativePosition.South;
                } else if (currenty == targety && currentx < targetx) {
                    relativeposition = RelativePosition.SouthWest;
                } else if (currenty < targety && currentx < targetx) {
                    relativeposition = RelativePosition.West;
                } else if (currenty > targety && currentx == targetx) {
                    relativeposition = RelativePosition.NorthWest;
                } else if (currenty > targety && currentx > targetx) {
                    relativeposition = RelativePosition.North;
                } else if (currenty == targety && currentx > targetx) {
                    relativeposition = RelativePosition.NorthEast;
                } else if (currenty < targety && currentx > targetx) {
                    relativeposition = RelativePosition.East;
                }

                break;

            case West:

                if (currenty < targety && currentx == targetx) {

                    relativeposition = RelativePosition.East;
                } else if (currenty < targety && currentx < targetx) {
                    relativeposition = RelativePosition.SouthEast;
                } else if (currenty == targety && currentx < targetx) {
                    relativeposition = RelativePosition.South;
                } else if (currenty < targety && currentx < targetx) {
                    relativeposition = RelativePosition.SouthWest;
                } else if (currenty > targety && currentx == targetx) {
                    relativeposition = RelativePosition.West;
                } else if (currenty > targety && currentx > targetx) {
                    relativeposition = RelativePosition.NorthWest;
                } else if (currenty == targety && currentx > targetx) {
                    relativeposition = RelativePosition.North;
                } else if (currenty < targety && currentx > targetx) {
                    relativeposition = RelativePosition.NorthEast
                    ;
                }

                break;

            case NorthWest:

                if (currenty < targety && currentx == targetx) {

                    relativeposition = RelativePosition.NorthEast;
                } else if (currenty < targety && currentx < targetx) {
                    relativeposition = RelativePosition.East;
                } else if (currenty == targety && currentx < targetx) {
                    relativeposition = RelativePosition.SouthEast;
                } else if (currenty < targety && currentx < targetx) {
                    relativeposition = RelativePosition.South;
                } else if (currenty > targety && currentx == targetx) {
                    relativeposition = RelativePosition.SouthWest;
                } else if (currenty > targety && currentx > targetx) {
                    relativeposition = RelativePosition.West;
                } else if (currenty == targety && currentx > targetx) {
                    relativeposition = RelativePosition.NorthWest;
                } else if (currenty < targety && currentx > targetx) {
                    relativeposition = RelativePosition.North;
                }

                break;
        }
    }

    private void action() {

        DcMotor rightdrive = hardwareMap.dcMotor.get("right_drive");
        DcMotor leftdrive = hardwareMap.dcMotor.get("left_drive");

        rightdrive.setDirection(DcMotor.Direction.REVERSE);

        leftdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
        rightdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);

        compassSensor = (HiTechnicNxtCompassSensor) hardwareMap.compassSensor.get("compass");
        double fullpower = 1.0;

        switch(relativeposition) {


            case North:

                rightdrive.setTargetPosition((int) encodercounts);
                leftdrive.setTargetPosition((int) encodercounts);

                rightdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
                leftdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);

                rightdrive.setPower(fullpower);
                leftdrive.setPower(fullpower);
                break;

            case NorthEast:

                if (compassvalue > compassvalue - (90-angle)) {
                    rightdrive.setPower(-fullpower);
                    leftdrive.setPower(fullpower);
                }
                else {
                    rightdrive.setTargetPosition((int) encodercounts);
                    leftdrive.setTargetPosition((int) encodercounts);

                    rightdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
                    leftdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);

                    rightdrive.setPower(fullpower);
                    leftdrive.setPower(fullpower);
                }

                break;

            case East:

                if (compassvalue > compassvalue - 90) {
                    rightdrive.setPower(-fullpower);
                    leftdrive.setPower(fullpower);
                }
                else {
                    rightdrive.setTargetPosition((int) encodercounts);
                    leftdrive.setTargetPosition((int) encodercounts);

                    rightdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
                    leftdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);

                    rightdrive.setPower(fullpower);
                    leftdrive.setPower(fullpower);
                }

                break;

            case SouthEast:

                if (compassvalue > compassvalue - (90+angle)) {
                    rightdrive.setPower(-fullpower);
                    leftdrive.setPower(fullpower);
                }
                else {
                    rightdrive.setTargetPosition((int) encodercounts);
                    leftdrive.setTargetPosition((int) encodercounts);

                    rightdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
                    leftdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);

                    rightdrive.setPower(fullpower);
                    leftdrive.setPower(fullpower);
                }

                break;

            case South:

                if (compassvalue > compassvalue - 180) {
                    rightdrive.setPower(-fullpower);
                    leftdrive.setPower(fullpower);
                }
                else {
                    rightdrive.setTargetPosition((int) encodercounts);
                    leftdrive.setTargetPosition((int) encodercounts);

                    rightdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
                    leftdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);

                    rightdrive.setPower(fullpower);
                    leftdrive.setPower(fullpower);
                }

                break;

            case SouthWest:

                if (compassvalue < compassvalue + (90+angle) ) {
                    rightdrive.setPower(fullpower);
                    leftdrive.setPower(-fullpower);
                }
                else {
                    rightdrive.setTargetPosition((int) encodercounts);
                    leftdrive.setTargetPosition((int) encodercounts);

                    rightdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
                    leftdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);

                    rightdrive.setPower(fullpower);
                    leftdrive.setPower(fullpower);
                }

                break;

            case West:

                if (compassvalue < compassvalue + 90) {
                    rightdrive.setPower(fullpower);
                    leftdrive.setPower(-fullpower);
                }
                else {
                    rightdrive.setTargetPosition((int) encodercounts);
                    leftdrive.setTargetPosition((int) encodercounts);

                    rightdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
                    leftdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);

                    rightdrive.setPower(fullpower);
                    leftdrive.setPower(fullpower);
                }

                break;

            case NorthWest:

                if (compassvalue < compassvalue + (90-angle)) {
                    rightdrive.setPower(fullpower);
                    leftdrive.setPower(-fullpower);
                }
                else {
                    rightdrive.setTargetPosition((int) encodercounts);
                    leftdrive.setTargetPosition((int) encodercounts);

                    rightdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
                    leftdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);

                    rightdrive.setPower(fullpower);
                    leftdrive.setPower(fullpower);
                }

                break;

        }



    }



    private void smartmove() {
        distance(currentx, currenty, targetx, targety);
        angle(currentx, currenty, targetx, targety);
        facing();
        pointrelative();
        action();

        currentx = targetx;     //Sets target point to new current position
        currenty = targety;
    }

    private final ArrayList<Integer> Step = new ArrayList<>();

    @Override
    public void init() {


    }

    @Override
    public void loop() {



        int size = Step.size();
        for (int i=0; i<size; i++) {
            int element = Step.get(i);

            switch (element) {
                case 1:

                    currentx = 134;
                    currenty = 60;

                    targetx = 108;
                    targety = 60;

                    smartmove();
                    break;
                case 2:

                    targetx = 59.5;
                    targety = 107.5;

                    smartmove();

                    break;
            }
        }

    }



}

1 个答案:

答案 0 :(得分:0)

对不起,这不是一个很好的答案,但我无法发表评论

你的代码很长,有很多冗余(你应该避免这种情况,但这不是重点)所以你真的需要包含一些关于你得到的错误的东西,除了它&# 39; s nullpointer(stacktrace,image,如果必须的话)。在您的代码中展开您的字段对您来说也很难找到。

最好的猜测:它看起来并没有实例化你的枚举

public enum Facing {North, NorthEast, East, SouthEast, South, SouthWest, West, NorthWest}
private Facing facing;
//naming this facing is also a pretty bad idea when you have a facing() function
public enum RelativePosition {North, NorthEast, East, SouthEast, South, SouthWest, West, NorthWest}
private RelativePosition relativeposition;

但这不过是猜测,这真的很难读懂