CANNotInitializedException

We’ve been trying to get our CANJaguar code working, but it keeps returning the CANNotInitializedException.

Our code right now is

public void robotInit(){
     try {
          CANJaguar armMotor = new CANJaguar (4,       CANJaguar.ControlMode.kPosition);
     } catch (CANTimeoutException ex){
          ex.printStackTrace();
}

What the console says:

[Squawk VM] Version: 2011 FRC, Nov  5 2011, 14:34:13
[cRIO] FPGA Hardware GUID: 0x1394f6dc1feb42ec6910e5767ed1d22c
[cRIO] FPGA Software GUID: 0xa14c11bde4bb64aef6a86fc52a294cd9
[cRIO] DIO LoopTiming: 260, expecting: 261
[cRIO] edu.wpi.first.wpilibj.can.CANNotInitializedException
[cRIO]     at edu.wpi.first.wpilibj.can.CANExceptionFactory.checkStatus(CANExceptionFactory.java:44)
[cRIO]     at edu.wpi.first.wpilibj.can.JaguarCANDriver.sendMessage(JaguarCANDriver.java:36)
[cRIO]     at edu.wpi.first.wpilibj.CANJaguar.sendMessage(CANJaguar.java:574)
[cRIO]     at edu.wpi.first.wpilibj.CANJaguar.getTransaction(CANJaguar.java:637)
[cRIO]     at edu.wpi.first.wpilibj.CANJaguar.getFirmwareVersion(CANJaguar.java:1199)
[cRIO]     at edu.wpi.first.wpilibj.CANJaguar.initCANJaguar(CANJaguar.java:247)
[cRIO]     at edu.wpi.first.wpilibj.CANJaguar.<init>(CANJaguar.java:286)
[cRIO]     at edu.wpi.first.wpilibj.templates.RobotTemplate.robotInit(RobotTemplate.java:70)
[cRIO]     at edu.wpi.first.wpilibj.SimpleRobot.startCompetition(SimpleRobot.java:104)
[cRIO]     at edu.wpi.first.wpilibj.RobotBase.startApp(RobotBase.java:156)
[cRIO]     in virtual method #10 of javax.microedition.midlet.MIDlet(bci=17)
[cRIO]     at javax.microedition.midlet.MIDletTunnelImpl.callStartApp(64)
[cRIO]     at com.sun.squawk.imp.MIDletMainWrapper.main(110)
[cRIO]     in virtual method #95 of com.sun.squawk.Klass(bci=25)
[cRIO]     at com.sun.squawk.Isolate.run(1506)
[cRIO]     at java.lang.Thread.run(231)
[cRIO]     in virtual method #47 of com.sun.squawk.VMThread(bci=42)
[cRIO]     in static method #3 of com.sun.squawk.VM(bci=6)
[cRIO] WARNING: Robots don't quit!
[cRIO] ---> The startCompetition() method (or methods called by it) should have handled the exception above.
[cRIO] task 0x25c7bb8 (System Web Services Load Thread) deleted: errno=0 (0) status=0 (0)
[cRIO] task 0x28784f8 (mDNS resolve) deleted: errno=0 (0) status=0 (0)
[cRIO] task 0x2bff230 (mDNS resolve) deleted: errno=0 (0) status=0 (0)

We’d love your help. Thanks!

Also, you cannot define a jaguar inside of a code block (in this case, your “try” block, and then use it anywhere outside of the block. Outside of any method, but inside your class, type (you may have already done this):

CANJaguar armMotor;

Then, change your code that you displayed to this:
(Basically remove the CANJaguar variable marker):

public void robotInit(){
     try {
          armMotor = new CANJaguar (4,       CANJaguar.ControlMode.kPosition);
     } catch (CANTimeoutException ex){
          ex.printStackTrace();
}

Essentially, what is happening is that you’re not initializing your global CANJaguar variable. How Java works is that it allows global and local variables of the same name. So the Jaguar that you’re initializing isn’t the same one that you’re using at line 70. You made a new Jaguar also called “armMotor” and initialized that one. Because it had a conflicting name, Java resolved this by “hiding” the global variable from that function. The armMotor inside robotInit() was discarded after robotInit ended. The other armMotor that you defined elsewhere was never touched. By removing the CANJaguar reference, you’re not creating a new variable anymore but instead initializing the existing global one, which is what you intended on doing.

The described error will occur if the cRIO has not had a CAN plugin set when the firmware was flashed. You need to re-image the cRIO to solve the problem.

AriTheHun, I believe you would be correct if the issue were a CANTimeoutException, but this is a CANNotInitializedException, which, as Metis said, is supposed to occur when the CAN Driver is not properly loaded.

This would actually cause a compilation error, not a runtime error as the jaguar object would be out of scope when you tried to use it.

So does it work if you select a CAN driver in the imaging tool?