0

I have this typed DataRow:

import org.jdesktop.dataset.DataRow;

public class MainDataRow extends DataRow {
  private MainDataTable baseDataTable;

  protected MainDataRow(MainDataTable dt) {
    super(dt);
    this.baseDataTable = dt;
  }

  public int    getId()                           { return (int)    super.getValue(baseDataTable.getId()); };
  public void   setId(int id)                     {                 super.setValue(baseDataTable.getColId(), id); };
  public int    getDelta()                        { return (int)    super.getValue(baseDataTable.getColDelta()); };
  public void   setDelta(int delta)               {                 super.setValue(baseDataTable.getColDelta(), delta); };
  public String getNombre()                       { return (String) super.getValue(baseDataTable.getColNombre()); };
  public void   setNombre(String nombre)          {                 super.setValue(baseDataTable.getColNombre(), nombre); };

MainDataTable is well formed and is working fine. Now what I'm trying to do is to append a new row to MainDataTable:

MainDataTable dt = new MainDataTable(ds);
MainDataRow dr = (MainDataRow) dt.appendRow();

I'm getting ClassCastException. Where is the problem? Thanks.

Edit MainDataTable is a typed DataTable with no overriding on appendRow():

public class TypedDataTable<TypeOfRow> extends DataTable {
...
}

public class MainDataTable extends TypedDataTable<MainDataRow> {
...
}
Miquel
  • 8,339
  • 11
  • 59
  • 82

1 Answers1

0

appendRow() returns a DataRow object, not a MainDataRow object. This is why your cast is failing. Since MainDataRow extends DataRow, just do

MainDataRow dr = dt.appendRow();

And extend functionality from there.

mooles
  • 311
  • 2
  • 5
  • 11
  • This is true only if the **dynamic type** of the returned value is `DataRow`, and not `MainDataRow`. – amit Apr 27 '12 at 12:26
  • Just tried but then there is a "Type mismatch: cannot convert from DataRow to MainDataRow" – Miquel Apr 27 '12 at 12:43