imsweb / x12-parser

A Java parser for ANSI ASC X12 documents.
Other
80 stars 44 forks source link

Creating/Writing example from README.md produces IndexOutOfBoundsException #63

Closed jon-m-craig closed 2 years ago

jon-m-craig commented 2 years ago

(I am looking into it myself, but I wanted to let you know...)

The README.md example we've been talking about this morning produces IndexOutOfBoundsException when actually run.

java.lang.IndexOutOfBoundsException: Index: 1, Size: 0 at java.base/java.util.ArrayList.rangeCheckForAdd(ArrayList.java:788) ~[na:na] at java.base/java.util.ArrayList.add(ArrayList.java:513) ~[na:na] at com.imsweb.x12.Segment.addElement(Segment.java:190) ~[x12-parser-1.14.jar:1.14]

ctmay4 commented 2 years ago

I created a test that is working in SegmentTest. It uses the new helper method which is not released, but you could easily just replace the two string addElement with new Element(id, value). Please look at that. It runs properly in the integration tests.

@Test
void testReadmeExample() {
    Loop isaLoop = new Loop("ISA_LOOP");

    Segment segment = new Segment("ISA");
    segment.addElement("01", "00");
    segment.addElement("02", "          ");
    segment.addElement("03", "01");
    segment.addElement("04", "SECRET    ");
    segment.addElement("05", "ZZ");
    segment.addElement("06", "SUBMITTERS.ID  ");
    segment.addElement("07", "ZZ");
    segment.addElement("08", "RECEIVERS.ID   ");
    segment.addElement("09", "030101");
    segment.addElement("10", "1253");
    segment.addElement("11", "U");
    segment.addElement("12", "00501");
    segment.addElement("13", "000000905");
    segment.addElement("14", "1");
    segment.addElement("15", "T");
    segment.addElement("16", ":");
    isaLoop.addSegment(segment);

    segment = new Segment("IEA");
    segment.addElement("01", "1");
    segment.addElement("02", "000000905");
    isaLoop.addSegment(segment);

    assertEquals(2, isaLoop.getSegments().size());
}
jon-m-craig commented 2 years ago

I created a test that is working in SegmentTest. It uses the new helper method which is not released, but you could easily just replace the two string addElement with new Element(id, value). Please look at that. It runs properly in the integration tests.

@Test
void testReadmeExample() {
    Loop isaLoop = new Loop("ISA_LOOP");

    Segment segment = new Segment("ISA");
    segment.addElement("01", "00");
    segment.addElement("02", "          ");
    segment.addElement("03", "01");
    segment.addElement("04", "SECRET    ");
    segment.addElement("05", "ZZ");
    segment.addElement("06", "SUBMITTERS.ID  ");
    segment.addElement("07", "ZZ");
    segment.addElement("08", "RECEIVERS.ID   ");
    segment.addElement("09", "030101");
    segment.addElement("10", "1253");
    segment.addElement("11", "U");
    segment.addElement("12", "00501");
    segment.addElement("13", "000000905");
    segment.addElement("14", "1");
    segment.addElement("15", "T");
    segment.addElement("16", ":");
    isaLoop.addSegment(segment);

    segment = new Segment("IEA");
    segment.addElement("01", "1");
    segment.addElement("02", "000000905");
    isaLoop.addSegment(segment);

    assertEquals(2, isaLoop.getSegments().size());
}

The index parameter also has to be an int, not a String; I had to remove the quotes to even get it to compile.

I am still getting IndexOutOfBoundsException when using lines of the form,

    segment.addElement(1, new Element("00", "00"));
ctmay4 commented 2 years ago

That is a different issue. The elements are stored as a collection on the segment. You cannot add at a specific index if it doesn't already exist. To be honest, the addElement at a specific index method should probably just be removed.

Just use it without the index and if it is the first one it will be added at the first index.

segment.addElement(new Element("00", "00"));

This library has been used much more for reading X12 files. The APIs for creating new files could definitely be cleaned up.

jon-m-craig commented 2 years ago

That is a different issue. The elements are stored as a collection on the segment. You cannot add at a specific index if it doesn't already exist. To be honest, the addElement at a specific index method should probably just be removed.

Just use it without the index and if it is the first one it will be added at the first index.

segment.addElement(new Element("00", "00"));

This library has been used much more for reading X12 files. The APIs for creating new files could definitely be cleaned up.

Fair enough. I need to mainly create files, so I'll probably be extending the heck out of this. Things like automatically adding the SE for an ST with the proper count and such... that should all be automatic.

ctmay4 commented 2 years ago

I understand. As I said, we developed this because we needed to read different types of X12 files. The creation APIs were basically added to allow us to more easily unit test.