Ragged/Jagged Array Null Pointer Exception

251 views Asked by At

in the code that follows I take counter[] which has a lot of values within it that are 0, and determine the amount of 0 values.

With that I create the first half of a jagged array (counter3) that has a row for every line in counter[] with a value, and columns equal to the value within counter[].

Initializing with counter3[i] = new double[counter[i] did not succeed, so i created the class MakeJagArray, which takes a value and returns a double array of 0 values the size of the number given.

Setting the value of counter3[i][j] is a null pointer. Not sure why

    int counter2 = 0;
    for(int i=0;i<counter.length; i++){
        if (counter[i]>0){
            counter2++;
        }
    }

    double[][]counter3 =new double[counter2][];

    int i = 0;
    for(j=0;j<counter.length;j++){
        if(counter[j]>0){
            //MakeJagArray pHT = new MakeJagArray(counter[j]);
            counter3[i] = new double[counter[j]];
            //counter3[i] = pHT.go();
            i++;
        }
    }
    double q;
    for(i=0;i<counter3.length; i++){
        for(j=0; j<counter3.length; j++){
        counter3[i][j] = 0.0; //q=linespositions[i][j];
        }
    }


public class MakeJagArray {
    int num;
    public MakeJagArray(int num){
        this.num= num;
    }

    public double[] go(){
        double[]poop = new double[num];
        for(int i = 0; i<poop.length;i++){
            poop[i] = 0.0;
        }
        return poop;
    }
}
0

There are 0 answers