View Full Version : 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);
}
}
euhlmann
31-07-2016, 12:34
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
class MyClass {
Object thing = null;
public void init() {
thing.toString();
}
public static void main (String[] args) {
new MyClass().init();
}
}
and you get
Exception in thread "main" java.lang.NullPointerException
at MyClass.init(Main.java:5)
at MyClass.main(Main.java:9)
You know that line 5 caused the error, which is
thing.toString();
NullPointerExceptions occur when you try to access a field or method of an object that is null. In this case, we try to access a method on thing. That must mean thing is null.
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.
Thanks for your quick reply and help. Here are the logs on the phone:
https://drive.google.com/open?id=0B3d8bcXc7HqveHBReDV6VF9KR2s
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.
Large disclaimer - I know *nothing* about FTC APIs but, according to this JavaDoc: http://ftckey.com/apis/ftc/com/qualcomm/robotcore/hardware/HardwareMap.html
You should be able to initialize an empty Hardware Map like this:
private HardwareMap rd_hardwareMap = new HardwareMap();
What I *don't* know is if the hardware map is supposed to be created a different way to be pre-populated or something.
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.....
No. Your main class (Brain) is extending OpMode class which means it inherited everything the OpMode class has http://ftckey.com/apis/ftc/com/qualcomm/robotcore/eventloop/opmode/OpMode.html. One of the fields that OpMode class has is a variable:
HardwareMap hardwareMap
So the hardwareMap is already created for you. If you want the RobotDrive class has access to it, you need to pass the hardwareMap to it through its constructor, like this:
public class Brain
{
...
private RobotDrive rd = new RobotDrive(hardwareMap);
}
public class RobotDrive
{
private HardwareMap rd_hardwareMap;
public RobotDrive(HardwareMap hardwareMap)
{
rd_hardwareMap = hardwareMap;
}
}
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);
}
}
euhlmann
01-08-2016, 10:40
Again, it's difficult to help without a stack trace.
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/forumdisplay.php?159-Android-Studio
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);
}
}
vBulletin® v3.6.4, Copyright ©2000-2017, Jelsoft Enterprises Ltd.