How to get Network Table methods to work

Hi everyone, I’ve been trying to use the new Network Table classes but for some reason, I can’t get NetworkTable.getTable to work like last year. Can anyone tell me what to use instead?

https://wpilib.screenstepslive.com/s/currentCS/m/getting_started/l/801080-new-for-2018#network-tables

This should help you. The old way was deprecated, among many others.

We’re working on updated screensteps examples for the new API. Here’s a very brief example of using getTable and getEntry using the new API. Note that if you don’t want to update, the old API is deprecated, but still works just fine.


package edu.wpi.first.wpilibj.templates;

import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.networktables.NetworkTableEntry;
import edu.wpi.first.networktables.NetworkTableInstance;

public class EasyNetworkTableExample extends TimedRobot {

  NetworkTableEntry xEntry;
  NetworkTableEntry yEntry;

  public void robotInit() {
    NetworkTableInstance inst = NetworkTableInstance.getDefault();
    NetworkTable table = inst.getTable("datatable");
    xEntry = table.getEntry("X");
    yEntry = table.getEntry("Y");
  }

  double x = 0;
  double y = 0;

  public void teleopPeriodic() {
    xEntry.setDouble(x);
    yEntry.setDouble(y);
    x += 0.05;
    y += 1.0;
  }
}

The thing that took me a while to figure out without very good documentation is that you always get an entry, then you can retrieve or set its value. It’s not like the old and deprecated method where getting and setting were separate functions; you now have to get in order to set. For example, to get the double “value” from the table “table1”:

table.getTable("table1").getEntry("value").getDouble(0.00);

To set the same entry:

table.getTable("table1").getEntry("value").setNumber(desiredValue);