-3

I always get this error whenever i compile the code:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

I know that the error is in the array chosen[], but i think I initializated it correctly. Can anyone tell where the problem is?

 public  boolean[] chosen=new boolean[n];

 public void clear() {
     for (int k= 0; k < n; k++)
     {
    chosen[k] = false;

     }

I saw other answers on the same question , but none of them was helpful. Thank you.

Davide Lorenzo MARINO
  • 22,769
  • 4
  • 33
  • 48
Rii933
  • 69
  • 8
  • Bug is not in the provided code. There is only one way to fix those errors. See linked question. – Tunaki Feb 12 '16 at 14:33
  • I know this is a duplicate question , but none of the other answers was helpful to me – Rii933 Feb 12 '16 at 14:34
  • Is not a duplicate question. Is related on the sequence of operations performed. So it specific for this question. If you post again the question I can answer to it. – Davide Lorenzo MARINO Feb 12 '16 at 14:35
  • @Rii933 Just take a look at the java docs then: "Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array." –  Feb 12 '16 at 14:37
  • @LPK I hope that Rii933 knows what is an arrayOfBoundException and how to solve it. But the problem is subtle. It depends on the sequence of calls and on the default value of not setted variables. – Davide Lorenzo MARINO Feb 12 '16 at 14:39
  • @DavideLorenzoMARINO Yeah, and with a bit of understanding of his self-written code he is now able to find out why this error happens. –  Feb 12 '16 at 14:40
  • @LPK yes could be possible. But the reasons to close this question is wrong. This is not an already answered question. Is a complet new one. – Davide Lorenzo MARINO Feb 12 '16 at 14:41
  • I know what the exception means , I just don't know where exactly is the illegal index . Because it says that the problem is in the clear () method, in the chosen[k] = false; line – Rii933 Feb 12 '16 at 14:41
  • 1
    @Rii933 well, k is your illegal index then... It is the only one in this line –  Feb 12 '16 at 14:42
  • why is k illegal? I think there is no error in it. – Rii933 Feb 12 '16 at 14:45
  • 1
    Yes, it is a duplicate. This is not a "debugging my code" site. – Raedwald Feb 12 '16 at 14:53
  • @Raedwald is not a clear duplicate. It was reopened for this reason. If you find a different question that is similar to this one you are free to cast your close vote again. But check before if the related question is really similar to this one. – Davide Lorenzo MARINO Feb 12 '16 at 14:55
  • 1
    See http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems – Raedwald Feb 12 '16 at 14:56
  • And you really think that is a debugger related question?!? – Davide Lorenzo MARINO Feb 12 '16 at 14:57
  • 1
    All bugs in programs manifest due to a particular sequence of operations in a program. – Raedwald Feb 12 '16 at 14:57
  • So any question asked on stackoverflow can be closed and related to that question on debuggers? – Davide Lorenzo MARINO Feb 12 '16 at 14:59

1 Answers1

1

This problem is related to the sequence of operations performed:

  • You create the array choosen with the default value of the variable n that is 0 (when the object containing it was instantiated). Now the array has length 0.

  • After you set n to a value different from 0.

  • After you call the method clear(). In the method clear you try to access to an element not present in the array choosen because the array was previously created with size 0 and any modification to n doesn't affect the array.

Davide Lorenzo MARINO
  • 22,769
  • 4
  • 33
  • 48