Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   FTC NullPointerException (http://www.chiefdelphi.com/forums/showthread.php?t=149746)

dajp999 07-31-2016 07:35 AM

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 07-31-2016 12:34 PM

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();
        }
}

and you get
Code:

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
Code:

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.

dajp999 07-31-2016 05:38 PM

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.

bdaroz 07-31-2016 06:39 PM

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();
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.....

mikets 07-31-2016 08:12 PM

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
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:
Code:

public class Brain
{
    ...
    private RobotDrive rd = new RobotDrive(hardwareMap);
}

public class RobotDrive
{
    private HardwareMap rd_hardwareMap;

    public RobotDrive(HardwareMap hardwareMap)
    {
        rd_hardwareMap = hardwareMap;
    }
}


dajp999 08-01-2016 07:50 AM

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);
}

}

euhlmann 08-01-2016 10:40 AM

Re: FTC NullPointerException
 
Again, it's difficult to help without a stack trace.

mikets 08-01-2016 02:04 PM

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);

    }

}



All times are GMT -5. The time now is 08:21 AM.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi