Q:

"Index is out of bound" error in CPLEX

In the following code, I'm unable to run my program and the error "Exception from IBM ILOG Concert: IloIntRangeI::getValue(IloInt index): index is out of bound" pops out every time. 
The snippet of the code which I've written follows
/*mod file*/
int NX=...;
 range nx=1..NX;
 tuple x{
    int N;
    float a;
    float b; 
 }

 x elem_x[nx]=...;

 tuple y{
    int u;
    int v;
 }

 int L;
 execute{
    L=elem_x[1].N;
    for (var i in nx){
        L=Opl.maxl(elem_x[i].N,L);  
    }
 }

 y elem_y [nx][1..L]=...;

/*dat file*/
NX=3;
 elem_x=[<1,7,9>,<3,1.5,9.4>,<2,2.7,3.4>];
 elem_y=[[<2,3>,<3,4>,<3,6>],
         [<4,7>,<1,9>,<5,7>]
         [<0,4>,<9,3>,<2,2>]];

The program runs perfectly fine if I write "y elem_y[nx][1..3]" instead of "y elem_y[nx][1..L]".  How to remove this error as i always would not be knowing the value of L in case of a large program. 

A:

do not forget that the execute blocks tun after the .dat read! 
So in the .mod if you write
int NX=...;
 range nx=1..NX;
 tuple x{
    int N;
    float a;
    float b; 
 }

 x elem_x[nx]=...;

 tuple y{
    int u;
    int v;
 }

 int L=max(i in nx) elem_x[i].N;
// execute{
//    L=elem_x[1].N;
//    for (var i in nx){
//        L=Opl.maxl( ,L);  
//    }
// }

 y elem_y [nx][1..L]=...;

then your model is fine

