I’m having trouble populating a multi dimensional struct from a custom step in EI 4.6. I have a delimited list of account numbers as input. The custom step uses a simple StringTokenizer to parse each account number. When I try to populate out.accts[i].acct_num, I get the runtime error of ArrayIndexOutOfBoundsException. Any suggestions out there on how to populate my out multi dimensional struct?
Here is a snippet. Lets assume this step parsed an input string of 3 numbers. Instead of showing the parsing of the string, I just hard coded the values
customStepMethod$out out = new customStepMethod$out();
public mRefs mRefs = new mRefs[0]; is the culprit.
The code is allocating an array with 0 entries. This is a fun side-effect of the code that is automatically generated by EI–it doesn’t know how many to allocate nor what var to use to set the size. You need to do it yourself by hand.
Assuming you’ll have 3 elements every time, you can use this instead:
public mRefs mRefs = new mRefs[3];
If the size needs to be dynamic, you can allocate the array right before you do the assignments:
This is at least the second time this subtle thing has caught me.
When allocating the array, the code is allocating slots for the entries to be placed there, but does not allocate the objects themselves. So the code:
out.mRefs = new customStepMethod$out.mRefs[3];
doesn’t give 3 mRefs objects, it gives an array with 3 slots for mRefs objects. What makes things more confusing is the fact that common string arrays imply that objects are allocated:
works but in fact we’ve actually instantiated 2 new String objects and placed references for them within the array slots. So we must do the same for any other object. This code should do the trick: