I'm not sure what this error is or why it is happening. verything I've found online has nothing to do with TableViews. But when this method in my prisoner Class is accessed by my TableView it throws a InvocationTargetException
. This is my first time using a TableView.
public int getCellBlock() {
for (int i = 0; i < Main.getCellBlocks().size(); i++) //for each cellblock
for (int j = 0; j < Main.getCellBlocks().get(i).getCells().size(); j++) //for each cell
for (int k = 0; k < Main.getCellBlocks().get(i).getCells().get(j).getInmates().size(); k++)
if (Main.getCellBlocks().get(i).getCells().get(j).getInmates().get(k).equals(this.idNum)) {
return i;
}
return -1;
}
Cell (has two subclasses but im not accessing data in them with the above method):
import java.io.Serializable;
import java.util.ArrayList;
public class Cell implements Serializable {
private ArrayList<Integer> inmateIdNums;
private int capacity = 0;
Cell(int cap){
capacity = cap;
inmateIdNums = new ArrayList<>();
for (int i = 0; i < capacity; i++)
inmateIdNums.add(null);
}
public void add (int idNum){
for (int i = 0; i < capacity; i++){
if (inmateIdNums.get(i) == null) {
inmateIdNums.set(i, idNum);
break;
}
}
}
public void add (int idNum, int dur){}
public void add (int idNum, String reason){}
public ArrayList<Integer> getInmates(){ return inmateIdNums; }
public int getEmptyBunks(){
int emptyBunks = 0;
for (int i = 0; i < inmateIdNums.size(); i++){
if (inmateIdNums.get(i) == null)
emptyBunks++;
}
return emptyBunks;
}
}
CellBlock:
import java.io.Serializable;
import java.util.ArrayList;
public class CellBlock implements Serializable{
private String name;
private String type;
private int capacity;
private int occupancy = 0;
private ArrayList<Cell> cells;
CellBlock(int cap, String nName, String nType, int cellCapacity){
name = nName;
type = nType;
capacity = cap;
cells = new ArrayList<>(capacity);
if (type == "Maximum Security" || type == "Minimum Security") {
for (int i = 0; i < capacity; i++)
cells.add(new Cell(cellCapacity));
}
else if(type == "Infirmary"){
for (int i = 0; i < capacity; i++)
cells.add(new InfirmaryCell(cellCapacity));
}
else if(type == "Isolation"){
for (int i = 0; i < capacity; i++)
cells.add(new IsolationCell(cellCapacity));
}
}
public void addInmate(int cell, int inmateIdNum){
cells.get(cell-1).add(inmateIdNum);
occupancy++;
}
public void addInmate(int cell, int inmateIdNum, String reason){
cells.get(cell-1).add(inmateIdNum, reason);
occupancy++;
}
public void addInmate(int cell, int inmateIdNum, int duration){
cells.get(cell-1).add(inmateIdNum, duration);
occupancy++;
}
public void removeInmate(int inmateIdNum){
//search for inmate and remove from list
}
public ArrayList<Cell> getInmates(){ return cells; }
public boolean checkCapacity(){
if (capacity > occupancy)
return true;
return false;
}
public ArrayList<Cell> getCells(){ return cells; }
public ArrayList<String> getOpenCells(){
ArrayList<String> openCells = new ArrayList<>();
for (int i = 0; i < cells.size();i++){
if (cells.get(i).getEmptyBunks() > 0)
openCells.add(Integer.toString(i+1));
}
return openCells;
}
}
The Error:
I'm not sure which did it, but I fixed this myself. The loops were fine.
The problem was one of two things - the comparison, or the files where I had written the information (I am using binary files and ObjectInputStream
/ObjectOutputStream
).
In the Cell constructor I had:
for (int i = 0; i < capacity; i++)
inmateIdNums.add(null);
And to avoid comparing Integer
to null
I changed it to:
for (int i = 0; i < capacity; i++)
inmateIdNums.add(0);
This did not fix my issue. So I could only assume that someone where along the way something went wrong with one of my files being written while this crappy comparison was going on. As a last ditch effort I deleted the files I had been working with and ran my program fresh to create new files and add new entries.
Problem solved.