|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
FTC NullPointerException
I am a new FTC coach and I am trying to learn Java to support out team. I created 2 classes one which will act as coordinating class which extends OpMode and for now a class representing the RobotDrive. I have done a fair amount of testing and debugging including verifying names of devices in the config file but I still receive a NullPointerException. What am I doing wrong?!?!?!
Here are the 2 classes: *********************************** package com.qualcomm.ftcrobotcontroller.opmodes; import com.qualcomm.robotcore.eventloop.opmode.OpMode; public class Brain extends OpMode { private int statusDriveInteger = 76; private RobotDrive rd = new RobotDrive(); public void init (){ statusDriveInteger = rd.statusRobotDrive(1); telemetry.addData("here ",statusDriveInteger); } public void loop (){ rd.setPowerLeftRearDrive(1.0); } } ************************************** package com.qualcomm.ftcrobotcontroller.opmodes; import com.qualcomm.robotcore.hardware.DcMotor; import com.qualcomm.robotcore.hardware.HardwareMap; public class RobotDrive { int erroroccurred; DcMotor h_leftRearDrive; private HardwareMap rd_hardwareMap = null; public int statusRobotDrive (double p_leftRearDrive){ try { h_leftRearDrive = rd_hardwareMap.dcMotor.get("1"); } catch (Exception p_exception) { h_leftRearDrive = null; erroroccurred = 102; } return erroroccurred; } public void setPowerLeftRearDrive (double p_leftRearDrivePower) { h_leftRearDrive.setPower(p_leftRearDrivePower); } } |
|
#2
|
||||
|
||||
|
Re: FTC NullPointerException
You never initialize rd_hardwareMap
Also next time, the contents of the stack trace would be useful to post Typically, the 2nd line of a stack trace tells you which line caused the exception. For example, if you have code like Code:
class MyClass {
Object thing = null;
public void init() {
thing.toString();
}
public static void main (String[] args) {
new MyClass().init();
}
}
Code:
Exception in thread "main" java.lang.NullPointerException at MyClass.init(Main.java:5) at MyClass.main(Main.java:9) Code:
thing.toString(); It's also sometimes helpful to use a Java debugger. I'm not sure how FTC programming works but if you have a debugger available, you should use it. It will break (pause the program) on exceptions and let you inspect the contents of all the variables at that point. Last edited by euhlmann : 31-07-2016 at 12:55. |
|
#3
|
|||
|
|||
|
Re: FTC NullPointerException
Thanks for your quick reply and help. Here are the logs on the phone:
https://drive.google.com/open?id=0B3...HBReDV6VF9KR2s I guess where I am confused is I thought this would initialize the hardwaremap: private HardwareMap rd_hardwareMap = null; Thanks again for any help you can provide. |
|
#4
|
||||
|
||||
|
Re: FTC NullPointerException
Large disclaimer - I know *nothing* about FTC APIs but, according to this JavaDoc: http://ftckey.com/apis/ftc/com/qualc...rdwareMap.html
You should be able to initialize an empty Hardware Map like this: Code:
private HardwareMap rd_hardwareMap = new HardwareMap(); I found this GitHub repo that looks promising (https://github.com/ftctechnh/ftc_app), however the example code never creates the HardwareMap class variable. It looks inherited from OpMode.... Sorry I can't help more..... |
|
#5
|
||||
|
||||
|
Re: FTC NullPointerException
No. Your main class (Brain) is extending OpMode class which means it inherited everything the OpMode class has http://ftckey.com/apis/ftc/com/qualc...de/OpMode.html. One of the fields that OpMode class has is a variable:
Code:
HardwareMap hardwareMap Code:
public class Brain
{
...
private RobotDrive rd = new RobotDrive(hardwareMap);
}
public class RobotDrive
{
private HardwareMap rd_hardwareMap;
public RobotDrive(HardwareMap hardwareMap)
{
rd_hardwareMap = hardwareMap;
}
}
Last edited by mikets : 31-07-2016 at 20:21. |
|
#6
|
|||
|
|||
|
Re: FTC NullPointerException
Thanks again for all the replies. I believe I have added a constructor as Mike explained and passed the hardwareMap to RobotDrive and I still receive the same results: it goes to the catch block. If there is a better place for me ask the question than here I am would be be willing to move it but i appreciate your kindness.
Updated code: ********************************************* package com.qualcomm.ftcrobotcontroller.opmodes; import com.qualcomm.robotcore.eventloop.opmode.OpMode; public class Brain extends OpMode { private int statusDriveInteger = 76; private RobotDrive rd = new RobotDrive(hardwareMap); public void init (){ statusDriveInteger = rd.statusRobotDrive(); telemetry.addData("here ",statusDriveInteger); } public void loop (){ telemetry.addData("here pre",statusDriveInteger); rd.setPowerLeftRearDrive(1.0); telemetry.addData("here post",statusDriveInteger); } } *********************************************** package com.qualcomm.ftcrobotcontroller.opmodes; import com.qualcomm.robotcore.hardware.DcMotor; import com.qualcomm.robotcore.hardware.HardwareMap; public class RobotDrive { int erroroccurred; DcMotor h_leftRearDrive; private HardwareMap rd_hardwareMap; public RobotDrive(HardwareMap hardwareMap) { rd_hardwareMap = hardwareMap; } public int statusRobotDrive (){ try { h_leftRearDrive = rd_hardwareMap.dcMotor.get("1"); } catch (Exception p_exception) { h_leftRearDrive = null; erroroccurred = 102; } return erroroccurred; } public void setPowerLeftRearDrive (double p_leftRearDrivePower) { h_leftRearDrive.setPower(p_leftRearDrivePower); } } |
|
#7
|
||||
|
||||
|
Re: FTC NullPointerException
Again, it's difficult to help without a stack trace.
|
|
#8
|
||||
|
||||
|
Re: FTC NullPointerException
Please try this instead. I think the initialization of rd is too early. Moving it to the init() method should work. BTW, this is primarily an FRC forum, you may get better response from the FTC forums http://ftcforum.usfirst.org/forumdis...Android-Studio
Code:
public class Brain extends OpMode {
private int statusDriveInteger = 76;
private RobotDrive rd;
public void init () {
rd = new RobotDrive(hardwareMap);
statusDriveInteger = rd.statusRobotDrive();
telemetry.addData("here ",statusDriveInteger);
}
public void loop (){
telemetry.addData("here pre",statusDriveInteger);
rd.setPowerLeftRearDrive(1.0);
telemetry.addData("here post",statusDriveInteger);
}
}
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|