I'm trying to implement an AbstractTableModel for a collection named "clients" but I keep receiving the error "required variable found value" for the "add" method.
Here is my code:
I'm sorry for the confusion created. The add method is meant to add a new client in the table (by that I mean a new row). I don't want to add a new client to the collection.
class ModelTabel extends AbstractTableModel{
public int getRowCount() {
return clients.size();
}
public int getColumnCount() {
return 4;
}
public Object getValueAt(int row, int column) {
Client c = clients.get(row-1);
switch(column){
case 0: return c.getName();
case 1: return c.getSurname();
case 2: return c.getID();
case 3: return c.getPhone();
default:return "ERROR";
}
}
public void add(Client c) {
clients.get(clients.size()++) = a;
fireTableDataChanged();
}
}
解决方案
I believe this is the same problem as this question... your variable assignment is reversed.
It should be (although this code is still incorrect - see below):
a = clients.get(clients.size()++);
EDIT: this was already answered by Prabhakaran, but apparently people felt the need to downvote my answer.. I think I did address the original question, but I appreciate that my code sample was still incorrect, so I will try to provide a more complete answer:
First of all, as to the "required variable not found" error, if you google it you will see that other SO question as the first hit. clients.get(clients.size()++) is not a variable, so you can't assign things to it. I am not sure where a is declared in your code, but assume that it is a variable and thus my suggestion of reversing the assignment.
Next, for the clients.get(clients.size()++) line, as others have mentioned or alluded to - VAR++ is equivalent to VAR = VAR + 1, so again is an assignment operation going on. In this case, clients.size() is not a variable, so you can not increment it. If you wanted the clients.size() + 1 index, you could have written: a = clients.get(clients.size() + 1)... but that will throw an ArrayIndexOutOfBoundsException, because you're trying to access an element of clients beyond its current size!
That is why Prabhakaran rewrote your method as they did, changing that line to a clients.add(c) call - as it seemed to fit the original intent of the method.