I´m using a Cplex Java API for the first time, usually I just use GAMS. I am trying to generate x[i] variable and an objective function that is a multiplication of x[k] and a parameter rl[k] which is of the type double. I always get an error that I am not understanding. Following is the code:
public void variable() throws IloException{
int i=0;
this.rl = new double [(int)Data.Count+1];
IloCplex cplex = new IloCplex();
IloNumVar[] x = new IloNumVar[Data.Count+1];
for(int Pnr = 0; Pnr <= Data.PCount; Pnr++)
{
if(Data.X[Pnr]==1)
{
i++;
x[i] = cplex.boolVar();
rl[i]=Data.RL[Pnr];
}
}
// Optimization problem
IloLinearNumExpr obj = cplex.linearNumExpr();
for(int k=0; k<=i; k++){
obj.addTerm(x[k], rl[k]);
}
cplex.addMaximize(obj);
}
and error:
Exception in thread "main" java.lang.NullPointerException
at ilog.cplex.CpxLinearExpr.unmarkVars(CpxLinearExpr.java:402)
at ilog.cplex.CpxLinearExpr.removeDuplicates(CpxLinearExpr.java:515)
at ilog.cplex.CpxLinearExpr.removeDuplicates(CpxLinearExpr.java:489)
at ilog.cplex.CpxObjective.setExpr(CpxObjective.java:115)
at ilog.cplex.CpxObjective.<init>(CpxObjective.java:369)
at ilog.cplex.IloCplexModeler.objective(IloCplexModeler.java:706)
at ilog.cplex.IloCplexModeler.addObjective(IloCplexModeler.java:768)
at ilog.cplex.IloCplexModeler.addMaximize(IloCplexModeler.java:810)
at ObjCplex.report1(ObjCplex.java:52)
at ObjCplex.<init>(ObjCplex.java:20)
at Main104.main(Main104.java:11)
You are leaving x[0] uninitialized due to a fencepost error. In your first for loop, i starts are 0, but you are incrementing it before you use it as an index so x1 is the first element of x that is not null. You don't need a second look and can just build the obj expression in the first loop. Also, it's idiomatic for for loops use strict < (or !=) as the continuation criteria instead of '<='.
You can just build the obj expression in the first loop, avoid two loops and avoid creating the array x
IloCplex cplex = new IloCplex();
int i = 0;
this.rl = new double [(int)Data.Count+1];
IloLinearNumExpr obj = cplex.linearNumExpr();
for(int Pnr = 0; Pnr <= Data.PCount; Pnr++)
{
if(Data.X[Pnr]==1)
{
IloNumVar x = cplex.boolVar();
obj.addTerm(Data.RL[Pnr], x);
this.rl[i++] = Data.RL[Pnr];
}
}
cplex.addMaximize(obj);