javasapjco3

Getting error attempting to Iterate over a JCoTable using java.util.iterator


Am trying to implement some Java patterns (Facade, and Iterator) against JCo objects.

Here, I'm trying to create an Iterator over the JCoTable much like I would any collection in Java. Below is my class snippet. In MyClass I have an inner class that performs the iteration.

I'm using the table.getStructure(...) methods, but am missing something. I've seen some snippets where they're explicitly creating a structure using metadata, but honestly, the code is not present in those examples, and I struggle a little there, worrying that I'm trading one complexity for another.

My goal is to simplify SAP BAPI calls and table handling for other Java programmers. This is a refactoring exercise. My return variables are incompatible in the Iterator.

Thank you in advance.

My class:

class MyClass 
...
protected JCoTable table;
...

private class JCoTableIterator implements Iterator<JCoRecord> {

    public boolean hasNext() {
        return table.getRow() < table.getNumRows();
    }
    @Override
    public JCoRecord next() {
        return table.getStructure(table.getRow() + 1);
    }
    @Override
    public void remove() {
        table.deleteRow();
    }
}

My implementation:

Iterator<JCoRecord> it = table.iterator();
while(it.hasNext()) {
    JCoRecord record = it.next(); <<<<< *Exception
    logger.info(record.getString("WERKS"));
    ... other field procesing ... 
}  

But on the JCoRecord record = it.next() line I get the following error.

com.sap.conn.jco.ConversionException: (122) JCO_ERROR_CONVERSION: Cannot convert 
                                field VBELN of type CHAR to StructureRecord
    at com.sap.conn.jco.rt.AbstractRecord.createConversionException(AbstractRecord.java:415)
    at com.sap.conn.jco.rt.AbstractRecord.createConversionException(AbstractRecord.java:410)
    at com.sap.conn.jco.rt.AbstractRecord.getStructure(AbstractRecord.java:2472)  

Solution

  • Assuming that you want to iterate over the rows of the table, I'd say you're using JCoTable.getStructure() out of place. As the name suggests, it takes the nth field of the record that is currently selected and attempts to return it as a structure. This is of course only possible if that individual column is a structured column - if it is a single (atomic) field, then I'd expect this conversion to fail with exactly the error message you're encountering.