Null Pointer For Sendable Choosers

Hi! I have been working on my java robot code. It is Object Oriented, and I am using the Smart Dashboard for a GUI. I have tried to simulate the Robot Code, but I keep getting an error message that there is a Null Pointer Exception. I have set the Sendable chooser, which is what is giving me the error, to have a default option, but the error continues. The error message is

Error at frc.robot.Subsystems.GUI.getDrivingType(GUI.java:118): Unhandled exception: java.lang.NullPointerException
at frc.robot.Subsystems.GUI.getDrivingType(GUI.java:118)
at frc.robot.Subsystems.GUI.sayCurrentCommand(GUI.java:122)
at frc.robot.Robot.robotPeriodic(Robot.java:47)
at edu.wpi.first.wpilibj.IterativeRobotBase.loopFunc(IterativeRobotBase.java:328)
at edu.wpi.first.wpilibj.TimedRobot.startCompetition(TimedRobot.java:131)
at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:373)
at edu.wpi.first.wpilibj.RobotBase.lambda$startRobot$0(RobotBase.java:443)
at java.base/java.lang.Thread.run(Thread.java:829)

I have attached the source code below, and if anyone could tell me what is going wrong, and why the vs code says that there is a null pointer exception when I have set a default option, that would be ever so helpful. Thank you!

example.txt (221 Bytes) SwitchDrive.java (2.7 KB) Main.java (775 Bytes) Robot.java (2.7 KB) DriveTrain.java (1.4 KB) GUI.java (5.0 KB) Gyroscope.java (1.0 KB) OI.java (504 Bytes) PixyCam.java (595 Bytes) RobotMap.java (718 Bytes) Voltage.java (961 Bytes)

My suspicion is that initDefaultCommand() is not being called prior to getDrivingType(). The SendableChooser could return a null because of this, so for defensive coding you should add a null check to getDrivingType() that returns a default value.

In 22 of robot.java, I make a new instance of the GUI class. Shouldn’t this initialize the sendable chooser and add the options as well as the default options. I think Robot.java is the first class to get run and then in robotPeriodic of Robot.java, I reference the sayCurrentCommand() from my new object from line 22, which uses the getDrivingType() from the new GUI object. Is there something I am not seeing?

That is being done in initDefaultCommand(), not a static initializer or a constructor. Thus this function is not called at the time of creating the instance, but is called later by the command framework.

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