Code Error

This is the code:

/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018 FIRST. All Rights Reserved.                             */
/* Open Source Software - may be modified and shared by FRC teams. The code   */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project.                                                               */
/*----------------------------------------------------------------------------*/

package frc.robot;

import com.revrobotics.CANSparkMax;
import com.revrobotics.CANSparkMax.IdleMode;
import com.revrobotics.CANSparkMaxLowLevel.MotorType;

import edu.wpi.cscore.UsbCamera;
import edu.wpi.first.wpilibj.CameraServer;
import edu.wpi.first.wpilibj.Compressor;
import edu.wpi.first.wpilibj.DoubleSolenoid;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;

/**
 * The VM is configured to automatically run this class, and to call the
 * functions corresponding to each mode, as described in the TimedRobot
 * documentation. If you change the name of this class or the package after
 * creating this project, you must also update the build.gradle file in the
 * project.
 */
public class Robot extends TimedRobot {
  /**
   * This function is run when the robot is first started up and should be used
   * for any initialization code.
   */

    //***Controllers***
    public Joystick driver_Controller;
    public Joystick operator_Controller1;
    public Joystick operator_Controller2;

     //***Motors***
     public CANSparkMax left_ChassisMaster;
     public CANSparkMax left_ChassisSlave1;
     public CANSparkMax left_ChassisSlave2;
 
     public CANSparkMax right_ChassisMaster;
     public CANSparkMax right_ChassisSlave1;
     public CANSparkMax right_ChassisSlave2;

     //***Motors ID***
    public static final int left_MasterDeviceID = 6;
    public static final int left_Follower1DeviceID = 9;
    public static final int left_Follower2DeviceID = 11;

    public static final int right_MasterDeviceID = 1;
    public static final int right_Follower1DeviceID = 7;
    public static final int right_FollowerDeviceID = 10;

   //***Chassis***
  private DifferentialDrive driveTrain;

  //***Reversa***
  protected boolean reverse = false;

   //***Compressor***
   public Compressor airCompressor;

   //***Solenoids***
   //Gearbox
   public DoubleSolenoid gearbox;

   //Toggle
   boolean toggle1 = true;
   boolean toggle2 = true;

  @Override
  public void robotInit() {

     //***Joystics Port Assigned***
     driver_Controller = new Joystick(0);
     operator_Controller1 = new Joystick(1);
     operator_Controller1 = new Joystick(2);

    //***Motors Id Assigned***
    left_ChassisMaster = new CANSparkMax(left_MasterDeviceID, MotorType.kBrushless);
    left_ChassisSlave1 = new CANSparkMax(left_Follower1DeviceID, MotorType.kBrushless);
    left_ChassisSlave2 = new CANSparkMax(left_Follower2DeviceID, MotorType.kBrushless);

    right_ChassisMaster = new CANSparkMax(right_MasterDeviceID, MotorType.kBrushless);
    right_ChassisSlave1 = new CANSparkMax(right_Follower1DeviceID, MotorType.kBrushless);
    right_ChassisSlave2 = new CANSparkMax(right_FollowerDeviceID, MotorType.kBrushless);

    //***Follower Mode Activated***
    left_ChassisSlave1.follow(left_ChassisMaster);
    left_ChassisSlave2.follow(left_ChassisMaster);

    right_ChassisSlave1.follow(right_ChassisMaster);
    right_ChassisSlave2.follow(right_ChassisMaster);

    //***Amp Limit***
    left_ChassisMaster.setSmartCurrentLimit(40);
    left_ChassisSlave1.setSmartCurrentLimit(40);
    left_ChassisSlave2.setSmartCurrentLimit(40);

    right_ChassisMaster.setSmartCurrentLimit(40);
    right_ChassisSlave1.setSmartCurrentLimit(40);
    right_ChassisSlave2.setSmartCurrentLimit(40);

    //***Ramp Value***
    double ramp = 0.75;

    //***Ramp Rate***
    left_ChassisMaster.setOpenLoopRampRate(ramp);

    right_ChassisMaster.setOpenLoopRampRate(ramp);

    left_ChassisSlave1.setOpenLoopRampRate(ramp);
    left_ChassisSlave2.setOpenLoopRampRate(ramp);

    right_ChassisSlave1.setOpenLoopRampRate(ramp);
    right_ChassisSlave2.setOpenLoopRampRate(ramp);

    //***BrakeMode Selected***
    left_ChassisMaster.setIdleMode(IdleMode.kBrake);
    left_ChassisSlave1.setIdleMode(IdleMode.kBrake);
    left_ChassisSlave2.setIdleMode(IdleMode.kBrake);

    right_ChassisMaster.setIdleMode(IdleMode.kBrake);
    right_ChassisSlave1.setIdleMode(IdleMode.kBrake);
    right_ChassisSlave2.setIdleMode(IdleMode.kBrake);

    //***Air Compressor Id Assigned***
    airCompressor = new Compressor(0);

    //***AirCompressor Security***
    airCompressor.setClosedLoopControl(true);

    //***AirCompressor Start***
    airCompressor.start();

    //***States Values Assigned***
    airCompressor.enabled();
    airCompressor.getPressureSwitchValue();
    airCompressor.getCompressorCurrent();

    new Thread(() -> {
      UsbCamera c0 = CameraServer.getInstance().startAutomaticCapture();
        
      c0.setFPS(30);
      c0.setResolution(640, 480);
        
      try {
        Thread.sleep(20);
      } catch (InterruptedException e) {
      }
    }).start();

  }

  public void kart1()
  {
      //****************
      double x = (driver_Controller.getRawAxis(4));
      double y = -(driver_Controller.getRawAxis(1));

      double r = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
      if (r > 1) {r = 1;}
      
      double kLimiteX = 0.30;//75; //<<-- Sensibilidad de giro <<
      
      double t = ((r - Math.abs(x)) * (1 - kLimiteX)) + (r * kLimiteX);
      if (t > 1) { t = 1;}
      
      reverse = false;
      //***Foward & reverse***
      if(y < 0)
      {
          double temp = -t;
          t = -r;
          r = temp;
          
          reverse = true;
      }
      
      if (x < 0)
      {
          double temp = t;
          t = r;
          r = temp;
      }
      
      driveTrain.tankDrive(r,t);
  }

  public void kart2()
  {
      double x = (driver_Controller.getRawAxis(4));
      double y = -(driver_Controller.getRawAxis(1));
  
      double r = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
      if (r > 1) {r = 1;}
      
      double kLimiteX = 0.30;//75; //<<-- Sensibilidad de giro <<
      
      double t = ((r - Math.abs(x)) * (1 - kLimiteX)) + (r * kLimiteX);
      if (t > 1) { t = 1;}
      
      reverse = false;
      //***Foward & reverse***
      if(y < 0)
      {
          double temp = -t;
          t = -r;
          r = temp;
          
          reverse = true;
      }
      
      if (x < 0)
      {
          double temp = t;
          t = r;
          r = temp;
      }
      
      driveTrain.tankDrive((r * 0.50) , (t * 0.50));
  }

  @Override
  public void autonomousInit() {
  }

  @Override
  public void autonomousPeriodic() {
  }

  @Override
  public void teleopInit() {
  }

  @Override
  public void teleopPeriodic() {

    if(operator_Controller2.getRawButton(3)){
      if(toggle2 == true){
       toggle2 = false;
      }
     }
   
     if(operator_Controller2.getRawButton(4)){
       if(toggle2 == false){
         toggle2 = true;
       }
      }
  
    if(operator_Controller2.getRawButton(1)){
      if(toggle1 == true){
       toggle1 = false;
      }
     }
   
     if(operator_Controller2.getRawButton(2)){
       if(toggle1 == false){
         toggle1 = true;
       }
      }

      if(driver_Controller.getRawButtonPressed(1)){
        gearbox.set(DoubleSolenoid.Value.kForward);
      }
      if(driver_Controller.getRawButtonPressed(2)){
        gearbox.set(DoubleSolenoid.Value.kReverse);
      }

    if(toggle2)
          {
            if(toggle1){
              kart1();
            } 
            else 
            {
              kart2();
            }
          }
          else
          {
            left_ChassisMaster.stopMotor();
            right_ChassisMaster.stopMotor();
          }

  }

  @Override
  public void testInit() {
  }

  @Override
  public void testPeriodic() {
  }

}

This is the error:

ERROR  1  Unable to retrieve SPARK MAX firmware version for CAN ID:  7. Please verify the deviceID field matches the configured CAN ID of the controller, and that the controller is connected to the CAN Bus.  com.revrobotics.CANSparkMaxLowLevel.<init>(CANSparkMaxLowLevel.java:206) 
ERROR  1  Unable to retrieve SPARK MAX firmware version for CAN ID:  10. Please verify the deviceID field matches the configured CAN ID of the controller, and that the controller is connected to the CAN Bus.  com.revrobotics.CANSparkMaxLowLevel.<init>(CANSparkMaxLowLevel.java:206) 
ERROR  1  Unhandled exception: java.lang.NullPointerException  frc.robot.Robot.teleopPeriodic(Robot.java:243) 
Warning at edu.wpi.first.wpilibj.RobotBase.startRobot(RobotBase.java:274): Robots should not quit, but yours did! 
ERROR  1  The startCompetition() method (or methods called by it) should have handled the exception above.  edu.wpi.first.wpilibj.RobotBase.startRobot(RobotBase.java:276) 
ERROR  1  Unable to retrieve SPARK MAX firmware version for CAN ID:  9. Please verify the deviceID field matches the configured CAN ID of the controller, and that the controller is connected to the CAN Bus.  com.revrobotics.CANSparkMaxLowLevel.<init>(CANSparkMaxLowLevel.java:206) 
ERROR  1  Unable to retrieve SPARK MAX firmware version for CAN ID:  11. Please verify the deviceID field matches the configured CAN ID of the controller, and that the controller is connected to the CAN Bus.  com.revrobotics.CANSparkMaxLowLevel.<init>(CANSparkMaxLowLevel.java:206) 
ERROR  1  Unable to retrieve SPARK MAX firmware version for CAN ID:  7. Please verify the deviceID field matches the configured CAN ID of the controller, and that the controller is connected to the CAN Bus.  com.revrobotics.CANSparkMaxLowLevel.<init>(CANSparkMaxLowLevel.java:206) 
ERROR  1  Unable to retrieve SPARK MAX firmware version for CAN ID:  10. Please verify the deviceID field matches the configured CAN ID of the controller, and that the controller is connected to the CAN Bus.  com.revrobotics.CANSparkMaxLowLevel.<init>(CANSparkMaxLowLevel.java:206)

Do you have spark maxes on cad id 7, 9, 10, and 11?

You assign operator_Controller1 twice and never instantiate operator_Controller2. You’re calling an uninstantiated variable in teleopPeriodic, resulting in a NullPointerException.

Someone change the ids. I corrected them.

Now I’m getting this error:

ERROR  1  Unhandled exception: java.lang.NullPointerException  frc.robot.Robot.teleopPeriodic(Robot.java:243) 
Warning at edu.wpi.first.wpilibj.RobotBase.startRobot(RobotBase.java:274): Robots should not quit, but yours did! 
ERROR  1  The startCompetition() method (or methods called by it) should have handled the exception above.  edu.wpi.first.wpilibj.RobotBase.startRobot(RobotBase.java:276) 
Warning at edu.wpi.first.wpilibj.IterativeRobotBase.printLoopOverrunMessage(IterativeRobotBase.java:273): Loop time of 0.02s overrun

Assuming you followed what @Prateek_M said earlier, I would go through each Spark Max, make sure that they are on the latest version (1.1.33) and check that each ID is what exactly you want it to be.

If that does not work it’s likely a good idea to do a quick rewrite. And this time deploy in chunks, not adding all of it at once.

The max error is already solved, now the only error is the one above

Line 243 is this:
if(operator_Controller2.getRawButton(3))…

This means either your operator_Controller2 is not instantiated or that button is non-existent and is returning null. In line 81, you should switch operator_Controller1 to operator_Controller2 if you haven’t already fixed that.

Im still getting the error:

/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018 FIRST. All Rights Reserved.                             */
/* Open Source Software - may be modified and shared by FRC teams. The code   */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project.                                                               */
/*----------------------------------------------------------------------------*/

package frc.robot;

import com.revrobotics.CANSparkMax;
import com.revrobotics.CANSparkMax.IdleMode;
import com.revrobotics.CANSparkMaxLowLevel.MotorType;

import edu.wpi.cscore.UsbCamera;
import edu.wpi.first.wpilibj.CameraServer;
import edu.wpi.first.wpilibj.Compressor;
import edu.wpi.first.wpilibj.DoubleSolenoid;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;

/**
 * The VM is configured to automatically run this class, and to call the
 * functions corresponding to each mode, as described in the TimedRobot
 * documentation. If you change the name of this class or the package after
 * creating this project, you must also update the build.gradle file in the
 * project.
 */
public class Robot extends TimedRobot {
  /**
   * This function is run when the robot is first started up and should be used
   * for any initialization code.
   */

    //***Controllers***
    public Joystick driver_Controller;
    public Joystick operator_Controller1;
    public Joystick operator_Controller2;

     //***Motors***
     public CANSparkMax left_ChassisMaster;
     public CANSparkMax left_ChassisSlave1;
     public CANSparkMax left_ChassisSlave2;
 
     public CANSparkMax right_ChassisMaster;
     public CANSparkMax right_ChassisSlave1;
     public CANSparkMax right_ChassisSlave2;

     //***Motors ID***
    public static final int left_MasterDeviceID = 6;
    public static final int left_Follower1DeviceID = 9;
    public static final int left_Follower2DeviceID = 11;

    public static final int right_MasterDeviceID = 1;
    public static final int right_Follower1DeviceID = 7;
    public static final int right_FollowerDeviceID = 10;

   //***Chassis***
  private DifferentialDrive driveTrain;

  //***Reversa***
  protected boolean reverse = false;

   //***Compressor***
   public Compressor airCompressor;

   //***Solenoids***
   //Gearbox
   public DoubleSolenoid gearbox;

   //Toggle
   boolean toggle1 = true;
   boolean toggle2 = true;

  @Override
  public void robotInit() {

     //***Joystics Port Assigned***
     driver_Controller = new Joystick(0);
     operator_Controller1 = new Joystick(1);
     operator_Controller2 = new Joystick(2);

    //***Motors Id Assigned***
    left_ChassisMaster = new CANSparkMax(left_MasterDeviceID, MotorType.kBrushless);
    left_ChassisSlave1 = new CANSparkMax(left_Follower1DeviceID, MotorType.kBrushless);
    left_ChassisSlave2 = new CANSparkMax(left_Follower2DeviceID, MotorType.kBrushless);

    right_ChassisMaster = new CANSparkMax(right_MasterDeviceID, MotorType.kBrushless);
    right_ChassisSlave1 = new CANSparkMax(right_Follower1DeviceID, MotorType.kBrushless);
    right_ChassisSlave2 = new CANSparkMax(right_FollowerDeviceID, MotorType.kBrushless);

    //***Follower Mode Activated***
    left_ChassisSlave1.follow(left_ChassisMaster);
    left_ChassisSlave2.follow(left_ChassisMaster);

    right_ChassisSlave1.follow(right_ChassisMaster);
    right_ChassisSlave2.follow(right_ChassisMaster);

    //***Amp Limit***
    left_ChassisMaster.setSmartCurrentLimit(40);
    left_ChassisSlave1.setSmartCurrentLimit(40);
    left_ChassisSlave2.setSmartCurrentLimit(40);

    right_ChassisMaster.setSmartCurrentLimit(40);
    right_ChassisSlave1.setSmartCurrentLimit(40);
    right_ChassisSlave2.setSmartCurrentLimit(40);

    //***Ramp Value***
    double ramp = 0.75;

    //***Ramp Rate***
    left_ChassisMaster.setOpenLoopRampRate(ramp);

    right_ChassisMaster.setOpenLoopRampRate(ramp);

    left_ChassisSlave1.setOpenLoopRampRate(ramp);
    left_ChassisSlave2.setOpenLoopRampRate(ramp);

    right_ChassisSlave1.setOpenLoopRampRate(ramp);
    right_ChassisSlave2.setOpenLoopRampRate(ramp);

    //***BrakeMode Selected***
    left_ChassisMaster.setIdleMode(IdleMode.kBrake);
    left_ChassisSlave1.setIdleMode(IdleMode.kBrake);
    left_ChassisSlave2.setIdleMode(IdleMode.kBrake);

    right_ChassisMaster.setIdleMode(IdleMode.kBrake);
    right_ChassisSlave1.setIdleMode(IdleMode.kBrake);
    right_ChassisSlave2.setIdleMode(IdleMode.kBrake);

    //***Air Compressor Id Assigned***
    airCompressor = new Compressor(0);

    //***AirCompressor Security***
    airCompressor.setClosedLoopControl(true);

    //***AirCompressor Start***
    airCompressor.start();

    //***States Values Assigned***
    airCompressor.enabled();
    airCompressor.getPressureSwitchValue();
    airCompressor.getCompressorCurrent();

    new Thread(() -> {
      UsbCamera c0 = CameraServer.getInstance().startAutomaticCapture();
        
      c0.setFPS(30);
      c0.setResolution(640, 480);
        
      try {
        Thread.sleep(20);
      } catch (InterruptedException e) {
      }
    }).start();

  }

  public void kart1()
  {
      //****************
      double x = (driver_Controller.getRawAxis(4));
      double y = -(driver_Controller.getRawAxis(1));

      double r = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
      if (r > 1) {r = 1;}
      
      double kLimiteX = 0.30;//75; //<<-- Sensibilidad de giro <<
      
      double t = ((r - Math.abs(x)) * (1 - kLimiteX)) + (r * kLimiteX);
      if (t > 1) { t = 1;}
      
      reverse = false;
      //***Foward & reverse***
      if(y < 0)
      {
          double temp = -t;
          t = -r;
          r = temp;
          
          reverse = true;
      }
      
      if (x < 0)
      {
          double temp = t;
          t = r;
          r = temp;
      }
      
      driveTrain.tankDrive(r,t);
  }

  @Override
  public void autonomousInit() {
  }

  @Override
  public void autonomousPeriodic() {
  }

  @Override
  public void teleopInit() {
  }

  @Override
  public void teleopPeriodic() {

    if(operator_Controller2.getRawButton(3)){
      if(toggle2 == true){
       toggle2 = false;
      }
     }
   
     if(operator_Controller2.getRawButton(4)){
       if(toggle2 == false){
         toggle2 = true;
       }
      }
  
    if(operator_Controller2.getRawButton(1)){
      if(toggle1 == true){
       toggle1 = false;
      }
     }
   
     if(operator_Controller2.getRawButton(2)){
       if(toggle1 == false){
         toggle1 = true;
       }
      }

      if(driver_Controller.getRawButtonPressed(1)){
        gearbox.set(DoubleSolenoid.Value.kForward);
      }
      if(driver_Controller.getRawButtonPressed(2)){
        gearbox.set(DoubleSolenoid.Value.kReverse);
      }
            
      kart1();
  }

  @Override
  public void testInit() {
  }

  @Override
  public void testPeriodic() {
  }

}

Error:

ERROR  1  Unhandled exception: java.lang.NullPointerException  frc.robot.Robot.kart1(Robot.java:191) 
Warning at edu.wpi.first.wpilibj.RobotBase.startRobot(RobotBase.java:274): Robots should not quit, but yours did! 
ERROR  1  The startCompetition() method (or methods called by it) should have handled the exception above.  edu.wpi.first.wpilibj.RobotBase.startRobot(RobotBase.java:276) 
Warning at edu.wpi.first.wpilibj.IterativeRobotBase.printLoopOverrunMessage(IterativeRobotBase.java:273): Loop time of 0.02s overrun

If you look at your error:
ERROR 1 Unhandled exception: java.lang.NullPointerException frc.robot.Robot.kart1(Robot.java:191)

The line that says nullpointer at “Robot.java:191”

That means in your Robot.java class, there is a nullPointerException on Line 191. Before the error was on Line 243. Go through each of the errors and find the line number that is causing your error.

In this case it is driveTrain.tankDrive(r,t);

I don’t see the error

Nvm, solved it

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.