SegFault
SegFault

Reputation: 2210

Strange java.lang.ArrayIndexOutOfBoundsException: -1

I have this line of code:

    if (gram.getVN().get(i) != gram.getAxiom() && 
lr1.getAutoma()[j][lr1.getColumn().indexOf(gram.getVN().get(i))].size() != 0) {
 //some actions

}

and it works fine. But if I do this:

System.out.println(lr1.getAutoma()[j][lr1.getColumn().indexOf(gram.getVN().get(i))].size());

        if (gram.getVN().get(i) != gram.getAxiom() && 
    lr1.getAutoma()[j][lr1.getColumn().indexOf(gram.getVN().get(i))].size() != 0) {
     //some actions

}

then I get that exception, and i can't understand why! Can anyone help me? Thank you..

Upvotes: 1

Views: 86

Answers (1)

JB Nizet
JB Nizet

Reputation: 691943

Simply because lr1.getColumn().indexOf(gram.getVN().get(i)) returns -1.

In the first snippet, this part is not even executed because && is a short-circuit operator, and the first condition (gram.getVN().get(i) != gram.getAxiom()) is false.

Upvotes: 4

Related Questions