I made a simple sieve of erastothenes program to calculate the sum of primes up to n for project euler. It works fine for 100, 1,000 , 10,000 and so on but when i do 1 or 2 million, it gives me this :
java.lang.ArrayIndexOutOfBoundsException: -2146737495
It doesn't happen for smaller numbers, i use boolean arrays.
boolean[] primes = new boolean[2000001];
    ArrayList<Integer> list = new ArrayList<Integer>();
    for (int i = 2; i < primes.length; i++){
        primes[i] = true;
    }
    boolean stop = false;
    int target = 2;
    int cycles = 1;
    while (!stop) {
        list.add(target);
        for (int i = target;; i ++) //
        {
            if (target*i >= primes.length ) {//
                break;
            }
            primes[target*i] = false;//
        }
        for (int i = target + 1; ; i ++) {
            if(i >= primes.length) {
                stop = true;
                break;
            }
             else if (primes[i]) {
                target = i;
                break;
            }
        }
        cycles ++;
    }
    int sum = 0;
        for (int i = 0; i < list.size(); i++) {
        sum += list.get(i);;
    }
My goal is not at all really to find the answer, but many times I have abandoned my code due to these types of errors. I'd like to find a source for them.