0

I am posting what I think is relevant code. In table class I have :

class Table extends AbstractTableModel {
   private List<String> columnHeaders;
   private List<Object> tableData;
   public Table(SortedSet<String> oznake, List<Object> aRows) {
      columnHeaders= new ArrayList<String>(oznake);
      tableData= new ArrayList<Object>(aRows);
      System.out.println("       tableData:" + tableData.size() + " "+ tableData);
   }

   public int getColumnCount() {
      return columnHeaders.size();
   }
   public int getRowCount() {
      return tableData.size();
   }

   public Object getValueAt(int row, int column) {
      List rowData = (List)(tableData.get(row));
      return (String)rowData.get(column);
   }

   public String getColumnName(int column) {
      return (String)(columnHeaders.get(column));
   }
}

In XmlRead I have:

public class XmlRead {
   public List<Object> getTable() {
      Map<String, String> rowMap = new LinkedHashMap<>();
      List<Object> aRows = new ArrayList<>();
                  rowMap.put(sOznaka, parser.getText());
                  aRows.add(Arrays.toString(rowMap.values().toArray(new String[rowMap.size()])));
      return (List<Object>)aRows;
   }
}

I get from table class:

tableData:3 [[2007-01-01, 27.485, 156.93, 0, 1.3170], [2019-05-06, 25.715, 0, 124.13, 1.1199], [2019-05-09, 25.718, 122.91, 0, 1.1193]]

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: class java.lang.String cannot be cast to class java.util.List (java.lang.String and java.util.List are in module java.base of loader 'bootstrap')

    at irose.IroseTable.getValueAt(IroseTable.java:71)

It looks as the problem is the line:

List rowData = (List)(tableData.get(row));

I can not figure it out why. Seems I have to get Objects out of List that have 3 Array.

[[2007-01-01, 27.485, 156.93, 0, 1.3170], [2019-05-06, 25.715, 0, 124.13, 1.1199], [2019-05-09, 25.718, 122.91, 0, 1.1193]]

How can I get Object out of that Array, that is, if I want just:

2007-01-01

How can I do that?

3
  • But you define Map<String, String> rowMap = new LinkedHashMap<>(); Commented Jun 3, 2019 at 9:32
  • 3
    I advise you to avoid List<Object>. In your case, List<List<String>> is what you seem to need. Commented Jun 3, 2019 at 9:33
  • Also, there is no need to allocate a new object, you can use the already allocated one by just saving the reference. Commented Jun 3, 2019 at 9:34

1 Answer 1

1

The ClassCastException means the object you retrieve from tableData.get(row) is of type String, and cannot be casted to a List object. You have 2 options to correct your problem:

-you can keep the String from tableData.get(row) and use this regular expression to retrieve the date ([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))

-or you can change the object contained inside the tableData, by changing the line: aRows.add(Arrays.toString(rowMap.values().toArray(new String[rowMap.size()]))); to

aRows.addAll(rowMap.values().toArray(new String[rowMap.size()]));

Sign up to request clarification or add additional context in comments.

3 Comments

I choose second option, but I get error: no suitable method found for addAll(String[]) aRows.addAll(rowMap.values().toArray(new String[rowMap.size()])); I will probably try List<List<String>> as bracco suggest
The List<List<String>> would Indeed be a lot cleaner, but I normally aRows.addAll(rowMap.values()) should work
Since bracco and you both agree about List<List<String>> I will use that . Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.