Hello! I am trying to use WPILib’s HolonomicDriveController, and when I call the calculate method, the code craches with a NullPointerException. All of the parameters are initialized, the HolonomicDriveController object is initialized, and I don’t know why this is happening, or how to solve it.
Here is the link to the GitHub repository with the code: https://github.com/ShakedMev/SwerveFollowTragectoryRepo
I am calling the method in FollowGeneratedTrajectoryCommand, which runs during autonomous.
Oh god, I did not see that. Thank you so much, this really helped a lot!!!
The stack trace begun at the startCompetition() method, and driveController.calculate() was as far as it got. I wonder why it didn’t say that PIDControllerY was null.
So the stack trace tells me that there’s a NullPointerException when I call calculate. How would I know whether it means driveController is null, or if it’s one of the method parameters?
So let’s assume your stack trace said HolonomicDriveController line 109
// Calculate feedback velocities (based on position error).
double xFeedback = m_xController.calculate(currentPose.getX(), poseRef.getX());
double yFeedback = m_yController.calculate(currentPose.getY(), poseRef.getY());
Those are lines 107-109. Line 109 has a NPE There are 3 objects being dereferenced (using the . method name after is called dereferencing) on line 109 the currentPose, the poseRef, and m_yController . The currentPose can’t be null because it was just used in line line above, the poseRef could not because it was just used in the line above so the only other object you are dereferencing is the m_yController so it must be what is null.
So basically, figure what could cause the problem by looking at everything that is deferenced there, and then just ruling out the things that aren’t null, until you find what is?
That is the gist. Hopefully there isn’t too much dereferencing going on a line of code. Not relevant to this problem but you can get a null pointer without any explicit dereferencing. It is quite rare though. I did write an article about it a few years ago Java and the Sweet Science
I was well trained to drive to the root cause so here we go with various quotes from the Internet in case you don’t think I’m an expert at screwing up this way (but, alas, I am).
if we copy the code from one place in our codebase to another, there’s a chance that we’ll copy tokens that are unnecessary or forget to change tokens that should have been changed.
When duplicating that code to create a new ..., we’re likely to forget to change the ..., which will break the intended behavior for the new ....
Is copying and pasting code dangerous? Should control-c and control-v be treated not as essential programming keyboard shortcuts, but registered weapons?
Our lead Java teacher never copy/paste but always types. Okay, he isn’t perfect but when he does a rare copy/paste he always says “Mr. Thomas said to never do this!”
The irony of copy/paste is the debugging time and frustration far exceed the time saved.