Go to Post I urge them to build the kitbot and repeateadly smash it into a concrete wall in order to sate their anger. It will soon wane when they manage to do more damage to the wall than to the kitbot. That is honestly the reason that we ended up using the kitframe this year. - Andrew Blair [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 31-07-2016, 07:35
dajp999 dajp999 is offline
Registered User
no team
 
Join Date: Jul 2016
Rookie Year: 2015
Location: USA
Posts: 3
dajp999 is an unknown quantity at this point
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);
}

}
Reply With Quote
  #2   Spotlight this post!  
Unread 31-07-2016, 12:34
euhlmann's Avatar
euhlmann euhlmann is offline
CTO, Programmer
AKA: Erik Uhlmann
FRC #2877 (LigerBots)
Team Role: Leadership
 
Join Date: Dec 2015
Rookie Year: 2015
Location: United States
Posts: 298
euhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud of
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.
__________________
Creator of SmartDashboard.js, an extensible nodejs/webkit replacement for SmartDashboard


https://ligerbots.org

Last edited by euhlmann : 31-07-2016 at 12:55.
Reply With Quote
  #3   Spotlight this post!  
Unread 31-07-2016, 17:38
dajp999 dajp999 is offline
Registered User
no team
 
Join Date: Jul 2016
Rookie Year: 2015
Location: USA
Posts: 3
dajp999 is an unknown quantity at this point
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.
Reply With Quote
  #4   Spotlight this post!  
Unread 31-07-2016, 18:39
bdaroz's Avatar
bdaroz bdaroz is offline
Programming Mentor
AKA: Brian Rozmierski
FRC #5881 (TVHS Dragons)
Team Role: Mentor
 
Join Date: Jan 2016
Rookie Year: 2016
Location: Albany, NY
Posts: 371
bdaroz has much to be proud ofbdaroz has much to be proud ofbdaroz has much to be proud ofbdaroz has much to be proud ofbdaroz has much to be proud ofbdaroz has much to be proud ofbdaroz has much to be proud ofbdaroz has much to be proud ofbdaroz has much to be proud of
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.....
Reply With Quote
  #5   Spotlight this post!  
Unread 31-07-2016, 20:12
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 667
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
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;
    }
}
__________________

Last edited by mikets : 31-07-2016 at 20:21.
Reply With Quote
  #6   Spotlight this post!  
Unread 01-08-2016, 07:50
dajp999 dajp999 is offline
Registered User
no team
 
Join Date: Jul 2016
Rookie Year: 2015
Location: USA
Posts: 3
dajp999 is an unknown quantity at this point
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);
}

}
Reply With Quote
  #7   Spotlight this post!  
Unread 01-08-2016, 10:40
euhlmann's Avatar
euhlmann euhlmann is offline
CTO, Programmer
AKA: Erik Uhlmann
FRC #2877 (LigerBots)
Team Role: Leadership
 
Join Date: Dec 2015
Rookie Year: 2015
Location: United States
Posts: 298
euhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud of
Re: FTC NullPointerException

Again, it's difficult to help without a stack trace.
__________________
Creator of SmartDashboard.js, an extensible nodejs/webkit replacement for SmartDashboard


https://ligerbots.org
Reply With Quote
  #8   Spotlight this post!  
Unread 01-08-2016, 14:04
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 667
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
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);

    }

}
__________________
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


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

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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