Log in

View Full Version : nullpointerexception when calling AxisCamera getImage()


Thundrio
18-01-2014, 18:14
We are trying to follow the sample code for vision provided with java, in order to get our own started.

We are getting a nullpointerexception when this line of code is run

ColorImage image = camera.getImage();

any advice on how to fix this / what is causing the exception? I can provide more detail if neccessary

Arhowk
18-01-2014, 18:19
Up near robotInit, make sure that the line initializing the camera isn't commented out.

Thundrio
18-01-2014, 18:21
Its not, and I think that would generate a different exception anyway.

BigJ
18-01-2014, 20:28
Its not, and I think that would generate a different exception anyway.

In Java, a NullPointerException happens when you try to do something (like call a method or access a public variable) with a variable that is currently null. On that line of code, the only variable being accessed is camera.

mwtidd
18-01-2014, 20:38
In Java, a NullPointerException happens when you try to do something (like call a method or access a public variable) with a variable that is currently null. On that line of code, the only variable being accessed is camera.

Yeah, I ran into this exact exception with one of my student's earlier today. It was a good opportunity to explain why null pointers occur. As you stated the camera in camera.{something} is the only thing that could be null in this case.

This is the robot init directly from the sample code:

public void robotInit() {
//camera = AxisCamera.getInstance(); // get an instance of the camera
cc = new CriteriaCollection(); // create the criteria for the particle filter
cc.addCriteria(MeasurementType.IMAQ_MT_AREA, AREA_MINIMUM, 65535, false);
}


This is what it should look like after you uncomment the camera line


public void robotInit() {
camera = AxisCamera.getInstance(); // get an instance of the camera
cc = new CriteriaCollection(); // create the criteria for the particle filter
cc.addCriteria(MeasurementType.IMAQ_MT_AREA, AREA_MINIMUM, 65535, false);
}


After this the camera should not be null. If it is still null however I would add a log line, something like this:


public void robotInit() {
System.out.println("construct the camera");
camera = AxisCamera.getInstance(); // get an instance of the camera
cc = new CriteriaCollection(); // create the criteria for the particle filter
cc.addCriteria(MeasurementType.IMAQ_MT_AREA, AREA_MINIMUM, 65535, false);
}


Make sure that line executes.

Remember the vision sample is based off SimpleRobot. If you are using something like IterativeRobot, etc this function may not be automatically called by wpi lib.

Adding that log line will let you know whether or not that code is even being executed.

mmaunu
19-01-2014, 01:49
A follow-up to lineskier's recommendation: print the camera variable right after initializing it inside of robotInit().


public void robotInit() {
camera = AxisCamera.getInstance(); // get an instance of the camera
System.out.println( "Printing camera: " + camera );
cc = new CriteriaCollection(); // create the criteria for the particle filter
cc.addCriteria(MeasurementType.IMAQ_MT_AREA, AREA_MINIMUM, 65535, false);
}


If camera is indeed null, as it almost certainly is, you will see this:
Printing camera: null

I am not sure what would make this return null...other than having a misconfigured Axis camera. If you open a web browser and type in a url of 10.xx.yy.11 (where the xxyy is 3673, your 4 digit team number), do you connect to the Axis camera? If not, then you will need to reconfigure your camera. Here is the WPI page for configuring an Axis camera:
http://wpilib.screenstepslive.com/s/3120/m/8559/l/89729-configuring-an-axis-camera

Good luck and post again if this doesn't help. Once you get it fixed, let us know what fixed it so that the thread becomes a resource for others.