1

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:

enter image description here

enter image description here

Aly
  • 57
  • 1
  • 9
  • The cause is a `NullPointerException` at line 68 of Prisoner.java. (You should indicate in the question which line is line 68.) This means you are calling a method on a reference that is `null`. Since you have code which chains together long sequences of method calls, you will probably have to unchain those to figure out what is null. – James_D Oct 26 '15 at 22:32
  • 1
    Please don't post screenshots of stack traces, instead copy and paste the stack trace text into your question. – jewelsea Oct 27 '15 at 00:16
  • @jewelsea I was going to do that but it was so long (longer than what is in the images) and there was no way for me to format all of that in any way that would have been easy on the eyes. – Aly Oct 27 '15 at 03:45

1 Answers1

0

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.

Aly
  • 57
  • 1
  • 9