I have been given a method that returns a two dimensional object array. How can I get the data from the object array using the method given below, and put in a table or label to view the data that is stored within? This is what I have tried with no success:
Object[][] diffs = comp.getDifferences(comp.getName());
for(Object diff : diffs){
table.addItem(diff);
}
This is the method I was given:
public Object[][] getDifferences(String table) {
List<Tuple<Object, Object>> difference = differences.get(table);
Object[][] array = new Object[difference.size()][2];
for (int i = 0; i < array.length; i++) {
array[i][0] = difference.get(i).item1();
array[i][1] = difference.get(i).item2();
}
return array;
}
Any suggestions?
UPDATE: 3:52 CST 3/17:
I've come up with this...
Table table = new Table();
Object[][] diffs = comp.getDifferences(comp.getName());
for(int i = 0; i < diffs.length; i++){
table.addItem(diffs[i][0]);
table.addItem(diffs[i][1]);
}
This also isn't working. I get an NPE that points to this line:
Object[][] array = new Object[difference.size()][2];
Here is the entire Compare(comp) class:
public class Compare implements Serializable {
private String name;
private Map<String, List<Tuple<Object, Object>>> differences = new HashMap<String, List<Tuple<Object, Object>>>();
public Compare(String name) {
this.name = name;
}
public Compare(byte[] bytes) throws IOException, ClassNotFoundException {
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInput ois = new ObjectInputStream(bis);
Compare compare = (Compare)ois.readObject();
this.name = compare.name;
this.differences = compare.differences;
bis.close();
ois.close();
}
public String getName() {
return name;
}
public boolean addDifference(String table, Object control, Object test) {
if (!differences.containsKey(table)) {
differences.put(table, new ArrayList<Tuple<Object, Object>>());
}
differences.get(table).add(new Tuple<Object, Object>(control, test));
return true;
}
public Object[][] getDifferences(String table) {
List<Tuple<Object, Object>> difference = differences.get(table);
Object[][] array = new Object[difference.size()][2];
for (int i = 0; i < array.length; i++) {
array[i][0] = difference.get(i).item1();
array[i][1] = difference.get(i).item2();
}
return array;
}
public byte[] toBytes() throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput oos = new ObjectOutputStream(bos);
oos.writeObject(this);
oos.flush();
byte[] bytes = bos.toByteArray();
bos.close();
oos.close();
return bytes;
}
private class Tuple<X, Y> implements Serializable {
private final X x;
private final Y y;
public Tuple(X x, Y y) {
this.x = x;
this.y = y;
}
public X item1() {
return this.x;
}
public Y item2() {
return this.y;
}
}
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
Compare foo = new Compare("Test Compare");
foo.addDifference("AGREEMENT", 89, 90);
foo.addDifference("AGREEMENT", "foo", "bar");
foo.addDifference("ACCOUNT", 123, "Dog");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("C:\\Users\\uttbm01\\Documents\\Export\\foo.cmp")));
oos.writeObject(foo);
oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("C:\\Users\\uttbm01\\Documents\\Export\\foo.cmp")));
Compare bar = (Compare)ois.readObject();
ois.close();
System.out.println();
}
}