Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: xml read to table problem in getValueAt

  1. #1
    Junior Member
    Join Date
    Jun 2019
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default xml read to table problem in getValueAt

    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. How can I cast String to List or should I do something else?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: xml read to table problem in getValueAt

    How can I cast String to List
    You can't. You can build a list with the desired String in it.

    What is in the String? Can it be split and the results be placed in the list?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2019
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: xml read to table problem in getValueAt

    I thought I can not, thanks. I also think I already build a list with the desired string. That is this, in the [[:
    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]]

    That output is in the String in the List. How can I split it in to 15 parts, not three?

    I tried next:

       public Object getValueAt(int row, int col) {
          for(List<Object> td : (List)tableData) {
             for(int i=1;  i < tableData.size();  i++) {
                List string = (List)tableData.get(i);
                String string = (String)tableData.get(i);
                return tableData.get(i);
             }
          }
       }


    but it gave me :
    src/irose/IroseTable.java:48: error: incompatible types: Object cannot be converted to List<Object>
          for(List<Object> td : (List)tableData) {
                                ^

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: xml read to table problem in getValueAt

    in the [[:
    That looks like the enclosing brackets for a 2 dim array. The print out looks like there are 3 rows with each row having 5 columns.
    If you want to put those 15 elements into a list, there will need to be code to handle the rows and columns.
    First split the 3 rows and then for data for each row split out the 5 columns.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2019
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: xml read to table problem in getValueAt

    Quote Originally Posted by Norm View Post
    First split the 3 rows and then for data for each row split out the 5 columns.
    To do that, i guess I have to solve the problem with: "incompatible types: Object cannot be converted to List<Object>". How do I solve this?
    Last edited by solomon73; June 3rd, 2019 at 02:57 AM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: xml read to table problem in getValueAt

    How do I solve this?
    Do the conversion yourself instead of using a cast.
    Write a method that takes whatever type the data is in the Object as input and returns a List.
    To see the datatype, call getClass() on the Object.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2019
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: xml read to table problem in getValueAt

    How do I do that?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: xml read to table problem in getValueAt

    How do I do that?
       System.out.println(tableData.getClass());  // show object's class

    When you know the object's class, you can write the code to extract the parts you want to put in the list.
    private List<theDataType> getList(Object tableData) {
        // create list from tableData and return it
       return createdList;
    }
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Code to read a table from different applications
    By olabanjitajudeen in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 22nd, 2013, 07:47 AM
  2. convert excel to xml and read the input from xml file
    By rahulruns in forum Object Oriented Programming
    Replies: 5
    Last Post: April 3rd, 2012, 11:13 AM
  3. Reading XML File using DOMParser and have problem with accessing xml
    By optiMystic23 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 21st, 2012, 02:22 PM
  4. getValueAt() method
    By Yamato in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 7th, 2010, 10:52 AM
  5. How to download records of a table in XML format
    By rangarajank in forum JDBC & Databases
    Replies: 1
    Last Post: May 15th, 2010, 10:49 AM

Tags for this Thread