Frc java errors

hello everyone,
When I deploy my program, even though I can build the program, the robot code does not come and I get these errors.
My program is as follows. https://github.com/wusled/aa1-Imported.git
I will be very happy if you review it and tell me my mistakes.



It looks like there’s a bug in your code, hence the NullPointerException at line 98 of Swerve.java, but the link to your code is broken.

Is the repository private on GitHub?

1 Like

i m sorry yıu can run it now https://github.com/wusled/aa1-Imported.git

The issue is here:


You are trying to create the odometry using the module positions before you define the modules. Swap the swerveOdometry and mSwerveMods definitions and it should work

3 Likes

I know the question has been answered, but here’s an example of the debugging process:

Here’s the offending line and the context:

96 public SwerveModulePosition[] getModulePositions(){
97    SwerveModulePosition[] positions = new SwerveModulePosition[4];
98    for(SwerveModule mod : mSwerveMods){
99        positions[mod.moduleNumber] = mod.getPosition();
100    }
101    return positions;
102 }

So, the first line of the stack trace is saying, “cannot read the array length <on line 98> because the array is null”. mSwerveMods is what I would guess to be the array, based on the context.

Searching the file, I find near the top:

21  private SwerveModule[] mSwerveMods;

Confirming that it is an array, and that it is not initialized on the same line as it is declared. Where is it initialized? Searching for that:

25  public Swerve() {
...
33   mSwerveMods =
34        new SwerveModule[] {
35          new SwerveModule(0, Constants.Swerve.Mod0.constants),
36          new SwerveModule(1, Constants.Swerve.Mod1.constants),
37          new SwerveModule(2, Constants.Swerve.Mod2.constants),
38          new SwerveModule(3, Constants.Swerve.Mod3.constants)
39        };
...
43   }

Which shows that mSwerveMods is supposed to be initialized in the constructor of Swerve. So, why is it not initialized by the time your code runs?

Looking back at the stack trace, the next line reads:

at frc.robot.subsystems.Swerve.<init>(Swerve.java:30)

says that getModulePositions was called from line 30.

So, expanding the context from earlier:

25  public Swerve() {
26   gyro = new Pigeon2(Constants.Swerve.pigeonID);
27    gyro.configFactoryDefault();
28    zeroGyro();
29
30    swerveOdometry = new SwerveDriveOdometry(Constants.Swerve.swerveKinematics, getYaw(),getModulePositions());
31   
32    
33  mSwerveMods =
34        new SwerveModule[] {
35          new SwerveModule(0, Constants.Swerve.Mod0.constants),
36          new SwerveModule(1, Constants.Swerve.Mod1.constants),
37          new SwerveModule(2, Constants.Swerve.Mod2.constants),
38          new SwerveModule(3, Constants.Swerve.Mod3.constants)
39        };
...
43   }

And the rest is covered by @Ankur-Raghavan 's post above.

4 Likes

Can you show with arrows on the image which command lines should be replaced?

swerveOdometry = new … should be moved under mSwerveMods = …};
That’s because you’re referencing getModulePositions() (which uses the modules from mSwerveMods} before creating mSwerveMods itself, resulting in a NullPointer error.

I will try this tomorrow. Thanks for your answers.
My other question is which line of code can I write instead of this old code?

Replace whenPressed with onTrue- those functions were renamed for 2023 and the “pressed”/“active” variants were deprecated.

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