Log in

View Full Version : NetworkTables Null Pointer Exception


sarangmittal
10-02-2014, 18:50
Hey guys,

We are trying to use RoboRealm to do our vision tracking, and we are able to write the values from RoboRealm onto the NetworkTables (we can see the value changing in TableViewer). However, when we try to run code that gets the value from the NetworkTable and use it in the code, we get a Nullpointer.


package edu.wpi.first.wpilibj.templates;

import edu.wpi.first.wpilibj.networktables.NetworkTable;
import edu.wpi.first.wpilibj.networktables2.type.NumberAr ray;
import edu.wpi.first.wpilibj.tables.TableKeyNotDefinedExc eption;

public class Vision {
NetworkTable table;
double COG_X;

public Vision(){
NetworkTable table = NetworkTable.getTable("TitanTable");
}
public double test(){
try {
COG_X = table.getNumber("COG_X");
return COG_X;
} catch (TableKeyNotDefinedException exp){
System.out.println("Exception caught");
return 0;
}

}
}

The null pointer pops up from the table.getNumber("COG_X") function. We are thinking its an issue of the code not finding anything named COG_X, but we don't know what to put instead.

Also, when I tried to look in the API for Network Tables, (in the 2013.0.589 update), it was missing. One other member of the team hadn't updated from 2013.0.427 yet, and the API had documentation for NetworkTables. Anyone know what's going on there?

Thanks for the help!

BradAMiller
10-02-2014, 21:18
There's a second overloaded getNumber() method that takes two parameters, the name and a default value. If it can't find the named value, then the default value is supplied instead of the error being thrown. Take a look at this example:

http://wpilib.screenstepslive.com/s/3120/m/7912/l/80205-writing-a-simple-networktables-program-in-c-and-java-with-a-java-client-pc-side

sarangmittal
11-02-2014, 14:18
Thanks for the suggestion, but that didn't work. We are still getting a null pointer.

Joe Ross
11-02-2014, 14:23
We are trying to use RoboRealm to do our vision tracking, and we are able to write the values from RoboRealm onto the NetworkTables (we can see the value changing in TableViewer). However, when we try to run code that gets the value from the NetworkTable and use it in the code, we get a Nullpointer.

if you posted a screenshot of the TableViewer, we might be able to help better.

sarangmittal
11-02-2014, 14:36
So we realized how dumb we were being after some frustration. Our mentor pointed out that we weren't constructing the vision class in our main class. That got rid of one error (Thank god for mentors). Also, in the code above we are creating 2 networktables, a subtle but fatal error.

Thanks for everyone who tried to help.