0

So here's the thing. I have a JTable in my frame displaying some data that can be edited. New rows can be added, old rows can be removed.

Now, if I start with a table populated with some data, it works fine. I'm able to delete rows, and that also deletes the rows from my data Vector<Vector>. However, when I add a row, the row is shown in my table, but the change is not reflected in the data.

Vector<Object> newQuestion = new Vector<Object>(3, 1);
newQuestion.add(question.getText());
newQuestion.add(answer.getText());
newQuestion.add(false);
model.addRow(newQuestion); // Update the model with new question

model is a DefaultTableModel. I tried model.fireTableDataChanged(); even though DTM fires that by itself, however that didn't work either.

Any pointers?

EDIT: What's interesting is, that if I start with some data in the table, and add a row, the change is reflected in the data as well.

EDIT 2: https://github.com/thekarangoel/YALT/blob/master/src/editDB.java From line 65 is what adds a row! To try, compile, run, File > Add new Database. Give a name. Add New Row.

EDIT 3: For this code: System.out.println("Data: " + data); Vector> modelData = model.getDataVector(); System.out.println("Data: " + modelData);

I get this:

Data: null
Data: [[w, a, false]]

The first if data from my Vector. The second is data in the Vector of `model. Why is this happening? When I add something to existing table, the file line also shows the change.

Karan Goel
  • 1,117
  • 1
  • 12
  • 24
  • How could the change not being reflected in the data? You're adding a row to the model, so it has this new row. Proof is that the table displays it. What is the code that you execute and that doesn't do what you think it should do? What do you expect this code to do, and what does it do instead? – JB Nizet Mar 24 '13 at 18:08
  • Well, I need to add rows to an empty table, ad then save the data in file. When I print the data on the console to debug, it shows up as null even after adding the rows. If I try to save the table to a file, I get a `NullPointerException`. The code is highly dependent on my other classes, so unfortunately I cannot provide a SSCCE. – Karan Goel Mar 24 '13 at 18:11
  • The code you provided is working, since the data is shown in the table. Why don't you show us the code which doesn't? a NullPointerException comes with a stack trace, a file name, and a line number, so you should be able to easily identify where the non-working code is, and what the problem is. – JB Nizet Mar 24 '13 at 18:13
  • Have you tried to print the data of `model` after row is added to `JTable` via `model` ? – Vishal K Mar 24 '13 at 18:13
  • @JBNizet Yes, the Exception is thrown when I try to write the data to a file, since the data is null. Yes, I tried printing the `Vector` after each row is added, but it's just null. – Karan Goel Mar 24 '13 at 18:15
  • @VishalK I'm not sure how to print the `model` itself since it doesn't have a `toString()`. However, yes, I tried printing the Vector after each row is added, but it's just null. – Karan Goel Mar 24 '13 at 18:16
  • To which class does the `model` variable refers to? `DefaultTableModel` or your own overridden version of DefaultTableModel class or something else? – Vishal K Mar 24 '13 at 18:18
  • @VishalK from my OP: `model` is a `DefaultTableModel`. – Karan Goel Mar 24 '13 at 18:19
  • toString() is in java.lang.Object. So every object has one. And if you don't show code, we can't help. It's as simple as that. It seems you're adding data to a model, and trying to save another model into a file, but without seeing the code, we can only guess. – JB Nizet Mar 24 '13 at 18:19
  • Oh wait, I have it on github. https://github.com/thekarangoel/YALT/blob/master/src/editDB.java line #65! – Karan Goel Mar 24 '13 at 18:22
  • Please see all my edits. Added some important info! – Karan Goel Mar 24 '13 at 18:28

1 Answers1

2

Look at how you create the data:

public Vector<Vector<Object>> convertMapToVector(Map<String, String> quesToAnsMap) {
    if (quesToAnsMap.size() > 0) {
        // the data field is initialized here
        data = new Vector<Vector<Object>>(quesToAnsMap.size(), 1); 
        ...
        return data;
    }
    // but not here
    return new Vector<Vector<Object>>();
}

My advices to make the code more robust:

  • don't use a data field. It's not useful since the data is contained in the model already. So, to get the data, you just need to get it from the model.
  • either make all your method take arguments and return values, or not take anything, return void, and initialize fields, but mixing both is confusing. The method above initializes a field and returns it, but the else clause only returns it without initializing it.
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Aha.. Nice catch sir! That fixed it! I still need to clean most of my code, this is just a prototype. But thanks for the advice. :) – Karan Goel Mar 25 '13 at 00:34