开发者

ArrayList.indexOf kills the code, but doesn't crash the program, nor is it catched

I've got this code

public String[][] checkBoxes(String[][] varden){

    ArrayList[][] tillatnaSiffror = new ArrayList[9][9];

    for(int i=0;i<9;i++){
        for(int ruta=0;ruta<9;ruta++){

            if(tillatnaSiffror[i][ruta] == null){
                tillatnaSiffror[i][ruta] = new ArrayList<Integer>();
                for(int add=1;add<=9;add++){
                    tillatnaSiffror[i][ruta].add(add);
                }
            }
            if(varden[i][ruta].equals("X")){
                for(int a=0;开发者_StackOverflowa<9;a++){
                    try {

                        System.out.println(tillatnaSiffror[i][ruta].indexOf(Integer.parseInt(varden[i][ruta])));
                        System.out.print("Testing");

                    } catch(Throwable n){
                        System.out.print("Throws exception");
                    }
                }
            }
        }
    }
}

tillatnaSiffror[i][ruta] is an ArrayList containing the numbers 1-9, and the variables i and ruta are from for loops that wrap around my code. The two-dimensional array varden contains strings with digits 1 to 9. The problem is, instead of printing the index I'm looking for, it does nothing. And it doesn't print "Testing" afterwards either, and it's not catched because of an exception either. I counted the amount of exceptions.

However, if I put in zero's like this:

System.out.println(tillatnaSiffror[0][0].indexOf(Integer.parseInt(varden[0][0])));

Then it prints out the index, and also the "Testing" text. Any ideas why it doesn't work with the variables? It's not an ArrayIndexOutOfBounds problem; the variables are correct.


Without the whole code and without knowing what exception is thrown, my only suggestion is to split that line into multiple statements and see where it pops. Then you'll probably be able to find the answer yourself.

String s = varden[i][ruta];
int i = Integer.parseInt(s);
List<Integer> l = tillatnaSiffror[i][ruta];
int idx = l.indexOf(i);
System.out.println(idx);

Edit:

if(varden[i][ruta].equals("X")){

and then inside that if:

Integer.parseInt(varden[i][ruta]))

Do you see the problem? BTW, you don't seem to use a of the third for anywhere, is that right?


Almost certainly, you're getting a runtime exception, which is unchecked - meaning the compiler doesn't warn you needs catching. The most likely runtime exceptions you are getting are ArrayIndexOutOfBoundsException or NumberFormatExcpetion.

To "fix" this, try catch (Throwable e) instead - that will definitely catch anything thrown inside the try. I suspect you are not actually catching Exception but some subclass thereof.


The code will either:

  • print 'Testing'
  • throw an Exception which is caught by the 'catch' block
  • or throw a Throwable that is not a subclass of Exception and that is caught elsewhere.

Not all Throwables are subclasses of Exception: Errors are subclasses of Throwable but not Exception.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜