Hi i am currently trying to access a json file using gson but i am having trouble reading the values when they are nested into a list, i am able to read the outer list and values but cannot read the drive values that are nested in pos
my json file looks like this
{
"pos":[
{
"x": 1.0,
"y": 1.0,
"theta": 1.0,
"drive":[{
"p": 1.0,
"i": 0.5
}]
},
{
"x": 2.0,
"y": 2.0,
"theta": 2.0,
"drive":[{
"p": 1.0,
"i": 0.5
}]
},
{
"x": 3.0,
"y": 3.0,
"theta": 3.0,
"drive":[{
"p": 1.0,
"i": 0.5
}]
}
]
}
I can get all the x, y, and theta values inside pos but when I try accessing the nested values in drive i get a null pointer exception
Exception in thread "main" java.lang.NullPointerException
at frc.robot.files.take2.JsonManager.main(JsonManager.java:63)
This is how I am looping to retrieve the values pls note I am excluding the setter and getters for the values in the lists
@SerializedName("pos")
private List<PosData> pos = null;
public List<PosData> getPositions() {
return pos;
}
public void setPositions(List<PosData> pos) {
this.pos = pos;
}
@SerializedName("drive")
private List<PidData> drive = null;
public List<PidData> getDrive() {
return drive;
}
public void setDrive(List<PidData> drive) {
this.drive = drive;
}
JsonManager manager = new Gson().fromJson(json, new TypeToken<JsonManager>() {}.getType());
List<PosData> posList = manager.getPositions();
List<Double> xList = new ArrayList<>();
List<Double> yList = new ArrayList<>();
List<Double> thetaList = new ArrayList<>();
List<PidData> driveList = manager.getDrive();
// List<List<Double>> driveList = new ArrayList<>();
List<Double> drivePID = new ArrayList<>();
//make parent list list for drivePID that isnt driveList
System.out.println(drivePID);
for (PosData pos : posList) {
xList.add(pos.getX());
yList.add(pos.getY());
thetaList.add(pos.getTheta());
for(PidData pid : driveList){
drivePID.add(pid.getP());
}
}
System.out.println(xList);
System.out.println(yList);
System.out.println(thetaList);
System.out.println(drivePID);
}
I have double checked that it works properly when they are not nested by just having my drive list in the json file and trying to read its values
{
"drive":[{
"p": 1.0,
"i": 0.5
}]
}
for(PidData pid : driveList){
drivePID.add(pid.getP());
}
Thanks for any help in advance!