-4

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();
}

}

PeachesToad
  • 159
  • 3
  • 15
  • What is this trying to compare? Is it comparing one String to another String? What is your expected result? – user681574 Mar 17 '17 at 20:09
  • 1
    The method is not returning two arrays. It's returning *one* two-dimensional array. You can access its members by providing row and column indices -- e.g., diffs[0][0] and diffs[0][1]. – Andy Thomas Mar 17 '17 at 20:13
  • The method does extract the "Names" elements from the difference List. And then adds these extracted names onto a double Array, where you can access each element by calling array[i][0] == array[i][1] and comparing them to see if they are the same elements. You can invoke == or .equal to compare two elements. – Juniar Mar 17 '17 at 20:35
  • Wow, already downvoted for asking a question? Awesome. Thanks for the commenters who are trying to help by providing some type of feedback before the downvote. Andy Thomas and Juniar, thanks. You guys are correct. Your response has guided me in the right direction. – PeachesToad Mar 17 '17 at 20:38
  • Also, I just want to list all of the differences and print to a table or label, or anything. That is all. Doesn't have to do any comparing. I just want to see what is there. The comparison will have already been done and the differences are stored in "comp". – PeachesToad Mar 17 '17 at 20:41
  • I don’t understand your question, sorry. That should be plenty of reason for downvoting (I didn’t — yet). – Ole V.V. Mar 17 '17 at 20:44
  • Thanks, I will re word it. – PeachesToad Mar 17 '17 at 20:44
  • @OleV.V. I have updated the verbiage of my question. Still no luck. Thanks guys. – PeachesToad Mar 17 '17 at 20:58
  • In `differences.get(table)`, what is `differences`? Is it possible that you could do a complete example without giving us too much code? See [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Ole V.V. Mar 17 '17 at 21:03
  • @OleV.V. private Map>> differences = new HashMap>>(); – PeachesToad Mar 17 '17 at 21:09
  • I added the entire Compare class ("comp") to the description above. – PeachesToad Mar 17 '17 at 21:12
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Ole V.V. Mar 18 '17 at 05:46

1 Answers1

0

I am not going through all of your code. The reason I can see why you get a NullPointerException in the line you say is that in difference.size() difference is null, so you are calling a method on a null. This in turn is most likely because the map I inquired about, differences, does not have a mapping with your table as a key (the less likely explanation is that the key is there, but the value associated with it is null).

If you are sure the key-value pair should be there (with a non-null value), you need to go through the code that put it in there to see why it didn’t. If on the other hand you don’t want to guarantee it’s there, you should do a null check on difference with an if-else. If difference is null, you may for example return new Object[0][] from your method.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161