0

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;
    }
}
AlvaroAV
  • 10,335
  • 12
  • 60
  • 91
loegare
  • 142
  • 1
  • 11
  • `for(j=0; j – ajb Nov 13 '14 at 17:06
  • 1
    Check and see whether `counter3[i]` gets initialized. – Thomas Nov 13 '14 at 17:07
  • 1
    @ajb that's an error but it would result in an IndexOutOfBoundsException instead of a NPE. – Thomas Nov 13 '14 at 17:12
  • A couple of tips: 1) Try using more meaningful names, this will make understanding your code easier. 2) Debug your code to see what is null and then why. 3) If you can use lists instead. – Thomas Nov 13 '14 at 17:13
  • struggling with debugger, but id bet that ajb is right, good catch. and most of these are really just placeholders, trying to get info into the right format, so thats why the names kinda blow – loegare Nov 13 '14 at 18:18
  • and that did in fact fix it, thanks! looks like i was just dumb, it was an index out of bounds, idk why i read null pointer – loegare Nov 13 '14 at 19:08

0 Answers0