HolonomicDriveController.calculate() method causes NullPointerException

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.

Any help would be much appreciated. Thanks!!!

1 Like

Can you share the stack trace for the null pointer exception?

1 Like

This. As code crashes are a fall from grace the stack trace is the road to redemption.

You create PIDControllerX twice and never PIDControllerY

The stack trace should have identified that it was PIDControllerY that was null.

This has more information on reading stack traces. Reading Stacktraces — FIRST Robotics Competition documentation

5 Likes

OMG nice, I looked at those lines so many times and couldn’t find it:

image

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.

The important part of the stack trace would be the line number of the calculate method, that would tell you what was null.

Right, so for a guess it said HolonomicDriveController.java line 109, which would have made it much easier to find.

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?

Basically - how do I know where exactly to look?

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?

Can do!

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

Oh that’s good to know. Thank you for the explanations!

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.

1 Like

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