you guys and gals all know by now… java, command based… lol
We want to display the navx MXP values on the Smart dashboard.
We have the board connected on the MXP port using the REV ribbon cable.
Navx is powered and everything looks OK.
I have a NavigatorSensor_Subsystem class.
We put this line in our public void teleopPeriodic() {
SmartDashboard.putNumber("imu-yaw", NavigationSensor_Subsystem.NavigationSensor._ahrs.getYaw());
code compile successfully, but every time we enable the robot, it goes to enable and within a few seconds, it display this message and disable itself…
ERROR 1 ERROR Unhandled exception: java.lang.NullPointerException at [org.usfirst.frc.team5860.robot.Robot.teleopPeriodic(Robot.java:187), edu.wpi.first.wpilibj.IterativeRobot.startCompetition(IterativeRobot.java:130), edu.wpi.first.wpilibj.RobotBase.main(RobotBase.java:247)] edu.wpi.first.wpilibj.RobotBase.main(RobotBase.java:249)
ERROR Unhandled exception: java.lang.NullPointerException at [org.usfirst.frc.team5860.robot.Robot.teleopPeriodic(Robot.java:187), edu.wpi.first.wpilibj.IterativeRobot.startCompetition(IterativeRobot.java:130), edu.wpi.first.wpilibj.RobotBase.main(RobotBase.java:247)]
WARNING: Robots don’t quit!
As soon as i comment that one line above, everything runs just fine.
Any help would be appreciated…
you guys and gals all know by now… java, command based… lol
We want to display the navx MXP values on the Smart dashboard.
We have the board connected on the MXP port using the REV ribbon cable.
Navx is powered and everything looks OK.
I have a NavigatorSensor_Subsystem class.
We put this line in our public void teleopPeriodic() {
SmartDashboard.putNumber("imu-yaw", NavigationSensor_Subsystem.NavigationSensor._ahrs.getYaw());
code compile successfully, but every time we enable the robot, it goes to enable and within a few seconds, it display this message and disable itself…
ERROR 1 ERROR Unhandled exception: java.lang.NullPointerException at [org.usfirst.frc.team5860.robot.Robot.teleopPeriodic(Robot.java:187), edu.wpi.first.wpilibj.IterativeRobot.startCompetition(IterativeRobot.java:130), edu.wpi.first.wpilibj.RobotBase.main(RobotBase.java:247)] edu.wpi.first.wpilibj.RobotBase.main(RobotBase.java:249)
ERROR Unhandled exception: java.lang.NullPointerException at [org.usfirst.frc.team5860.robot.Robot.teleopPeriodic(Robot.java:187), edu.wpi.first.wpilibj.IterativeRobot.startCompetition(IterativeRobot.java:130), edu.wpi.first.wpilibj.RobotBase.main(RobotBase.java:247)]
WARNING: Robots don’t quit!
As soon as i comment that one line above, everything runs just fine.
Any help would be appreciated…
The most likely hypothesis based upon the single line of code posted and the stack trace is that:
NavigationSensor_Subsystem.NavigationSensor._ahrs
is uninitialized.
Please review the code that initializes the “NavigationSensor_Subsystem.NavigationSensor._ahrs” variable, if that doesn’t lead to an answer please post (at a minimum) the code that initializes the _ahrs member variable.
here is the NavigationSensor Subsystem…
package org.usfirst.frc.team5860.robot.subsystems;
import edu.wpi.first.wpilibj.command.Subsystem;
import com.kauailabs.navx.frc.AHRS;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.PIDSource;
import edu.wpi.first.wpilibj.SPI;
import edu.wpi.first.wpilibj.PIDSourceType;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
public class NavigationSensor_Subsystem extends Subsystem {
public static class NavigationSensor extends Subsystem implements PIDSource {
public static AHRS _ahrs;
public void init() {
try {
/* Communicate w/navX MXP via the MXP SPI Bus. */
/* Alternatively: I2C.Port.kMXP, SerialPort.Port.kMXP or SerialPort.Port.kUSB */
/* See http://navx-mxp.kauailabs.com/guidance/selecting-an-interface/ for details. */
_ahrs = new AHRS(SPI.Port.kMXP);
} catch (RuntimeException ex ) {
DriverStation.reportError("Error instantiating navX MXP: " + ex.getMessage(), true);
}
}
public boolean isCalibrating() {
return _ahrs.isCalibrating();
}
public void zeroGyro() {
_ahrs.zeroYaw();
}
public double getGyro() {
return _ahrs.getYaw();
}
public double pidGet() {
return this.getGyro();
}
public void debug() {
/*SmartDashboard.putNumber("imu-yaw", _ahrs.getYaw());
SmartDashboard.putBoolean("imu-moving", _ahrs.isMoving());
SmartDashboard.putBoolean("imu-connected", _ahrs.isConnected());
SmartDashboard.putBoolean("imu-calibrating", _ahrs.isCalibrating());
SmartDashboard.putData("imu", _ahrs);*/
}
public void initDefaultCommand() { }
public PIDSourceType getPIDSourceType() {
return _ahrs.getPIDSourceType();
}
public void setPIDSourceType(PIDSourceType type) {
_ahrs.setPIDSourceType(type);
}
}
@Override
protected void initDefaultCommand() {
// TODO Auto-generated method stub
}
}
When is the init method called?
hummmmm… doe snot look like i am calling it at all…
keep in mind …newbies here and making a lot of assumption… Should we call this method in our robot.java class…
BTW… big fan here… was volunteer at worlds last year and i could not believe how calm your driver was on Einstein when your bot flipped…
allright… found this and was able to get this going…
https://www.chiefdelphi.com/forums/showthread.php?t=155083
thanks for the lead…
Glad to hear it!
Null pointer exceptions are very very common in Java code. They’re also very very bad, which is why your robot code blew up.
A common practice is to guard against these problems by checking object references to see if they are null before trying to use them.
For example:
if (NavigationSensor_Subsystem.NavigationSensor._ahrs != null) {
SmartDashboard.putNumber("imu-yaw", NavigationSensor_Subsystem.NavigationSensor._ahrs.getYaw());
}