Robot code not found: The startCompetition() method (or methods called by it) should have handled the exception above

  • Unhandled exception: java.lang.NullPointerException

  • Error at org.usfirst.frc3719.driveTrain2020.subsystems.ColorWheel.(ColorWheel.java:50): Unhandled exception: java.lang.NullPointerException

  • at org.usfirst.frc3719.driveTrain2020.subsystems.ColorWheel.(ColorWheel.java:50)

  • at org.usfirst.frc3719.driveTrain2020.Robot.robotInit(Robot.java:62)

  • at edu.wpi.first.wpilibj.TimedRobot.startCompetition(TimedRobot.java:64)

  • at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:276)

  • at edu.wpi.first.wpilibj.RobotBase.startRobot(RobotBase.java:348)

  • at org.usfirst.frc3719.driveTrain2020.Main.main(Main.java:31)

  • Robots should not quit, but yours did!

  • at edu.wpi.first.wpilibj.TimedRobot.startCompetition(TimedRobot.java:64)

  • at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:276)

  • The startCompetition() method (or methods called by it) should have handled the exception above.

  • at edu.wpi.first.wpilibj.RobotBase.startRobot(RobotBase.java:348)

  • at org.usfirst.frc3719.driveTrain2020.Main.main(Main.java:31)

  • Warning at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:291): Robots should not quit, but yours did!

  • Error at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:293): The startCompetition() method (or methods called by it) should have handled the exception above.
    robot code is here
    my code in robot.java is

//
// This file was generated by RobotBuilder. It contains sections of
// code that are automatically generated and assigned by robotbuilder.
// These sections will be updated in the future when you export to
// Java from RobotBuilder. Do not put any code or make any change in
// the blocks indicating autogenerated code or it will be lost on an
// update. Deleting the comments indicating the section will prevent
// it from being updated in the future.


package org.usfirst.frc3719.driveTrain2020;

import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.command.Scheduler;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import org.usfirst.frc3719.driveTrain2020.commands.*;
import org.usfirst.frc3719.driveTrain2020.subsystems.*;
import org.usfirst.frc3719.driveTrain2020.subsystems.ColorWheel;
import edu.wpi.first.wpilibj.GenericHID.Hand;
import edu.wpi.first.wpilibj.I2C;
import edu.wpi.first.wpilibj.util.Color;
import com.revrobotics.ColorSensorV3;
import com.revrobotics.ColorMatch;
import com.revrobotics.ColorMatchResult;
import edu.wpi.first.wpilibj.DriverStation;

/**
 * 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.properties file in 
 * the project.
 */
public class Robot extends TimedRobot {

    Command autonomousCommand;
    SendableChooser<Command> chooser = new SendableChooser<>();

    public static OI oi;
    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS
    public static DrivetrainSub drivetrainSub;
    public static FieldMinipulator fieldMinipulator;
    public static ColorWheel colorWheel;

    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS
    private final I2C.Port i2cPort = I2C.Port.kOnboard;
    private final ColorSensorV3 colorSensor = new ColorSensorV3(i2cPort); 
    private final ColorMatch colorMatcher = new ColorMatch();
    public static final Color kBlueTarget = ColorMatch.makeColor(0.143, 0.427, 0.429);
    public static final Color kGreenTarget = ColorMatch.makeColor(0.197, 0.561, 0.240);
    public static final Color kRedTarget = ColorMatch.makeColor(0.561, 0.232, 0.114);
    public static final Color kYellowTarget = ColorMatch.makeColor(0.361, 0.524, 0.113);
    public static Color colorCase;
    public static char fieldColor;
    /**
     * This function is run when the robot is first started up and should be
     * used for any initialization code.
     */
    @Override
    public void robotInit() {

        // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
        drivetrainSub = new DrivetrainSub();
        fieldMinipulator = new FieldMinipulator();
        colorWheel = new ColorWheel();

    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
    
        // OI must be constructed after subsystems. If the OI creates Commands
        //(which it very likely will), subsystems are not guaranteed to be
        // constructed yet. Thus, their requires() statements may grab null
        // pointers. Bad news. Don't move it.
        oi = new OI();

        // Add commands to Autonomous Sendable Chooser
        // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=AUTONOMOUS

        chooser.setDefaultOption("Autonomous Command", new AutonomousCommand());

    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=AUTONOMOUS
        SmartDashboard.putData("Auto mode", chooser);
    }

    /**
     * This function is called when the disabled button is hit.
     * You can use it to reset subsystems before shutting down.
     */
    @Override
    public void disabledInit(){

    }

    @Override
    public void disabledPeriodic() {
        Scheduler.getInstance().run();
    }

    @Override
    public void autonomousInit() {
        autonomousCommand = chooser.getSelected();
        // schedule the autonomous command (example)
        if (autonomousCommand != null) autonomousCommand.start();
    }

    /**
     * This function is called periodically during autonomous
     */
    @Override
    public void autonomousPeriodic() {
        Scheduler.getInstance().run();
    }

    @Override
    public void teleopInit() {
        // This makes sure that the autonomous stops running when
        // teleop starts running. If you want the autonomous to
        // continue until interrupted by another command, remove
        // this line or comment it out.
        if (autonomousCommand != null) autonomousCommand.cancel();
        colorMatcher.addColorMatch(kBlueTarget);
        colorMatcher.addColorMatch(kGreenTarget);
        colorMatcher.addColorMatch(kRedTarget);
        colorMatcher.addColorMatch(kYellowTarget);
    }

    /**
     * This function is called periodically during operator control
     */
    @Override
    public void teleopPeriodic() {
        Scheduler.getInstance().run();
       // Robot.drivetrainSub.SetDrive(Robot.oi.xboxController.getY(Hand.kLeft), Robot.oi.xboxController.getX(Hand.kRight));
       
    }
}```

Scroll up a bit, there should be more info on the exception.

A few suggestions:

  1. You need to encode your code entirely with the three ``` (3 backtick characters) .
  2. The lines in the stacktrace above where you pasted usually have the exact exception that is thrown, and the line number where it happened. Normally they are either NullPointerException or something related to instantiated objects of things on the same ports.
  3. If you want thorough help, best thing is to just push your code to github or similar, and post a link.

how do I push my code to github?

Here is a step by step walkthrough.

1 Like

Check this out.

code is here https://github.com/FlamingoStorm/Robot2020ColorSensor

Can you also send the full error output?

The top line of the stack trace you’ve posted points exactly to the problem. You’re calling a method on a null object. Check the stack trace, go to the indicated line, and see which object it is.

That is all you need to know where the problem is.

Go to your Colorwheel.java file, look at line 50. The object you reference on that line has not been instantiated properly, and is a NullPointer being dereferenced.

My recommendation to you for general code design practices that will help you eliminate these problems is practical.

When you design a class do the following in your source file:

  1. Define the class
  2. Declare all the member variables
  3. Instantiate ALL the member variables in your constructor → this is why you have your error
  4. Then, define all the other methods that will be used

That way, whenever a member variable is added, you immediately initialize it in the Constructor of the class and you avoid null pointers.

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