0

I am making a project in which I need to put a certain precision(0.00 or 0.000) on values in a 2d array. The 2d array is a String and i want to convert it to double. But when I try, I get a NullPointerException, I don't know why. Afterwards I want to again convert it to a String array.

print-method code:

public void printSheet() {
    int start = 'a';
    char letter = (char) start;
    //kolomnamen
    for (int i = 0; i < COLUMNS + 1; i++) {
        if (i == 0) {
            System.out.print("   ");
        } else if (WIDTH != 0) {
            String s = "";
            for (int j = 0; j < WIDTH / 2; j++) {
                s += "-";
            }
            s += (char) (letter + i - 1);
            for (int j = 0; j < WIDTH / 2; j++) {
                s += "-";
            }
            System.out.print(s + "\t");
        }
    }

    System.out.println("\n");
    for (int i = 0; i < sheet.length; i++) {
        System.out.print(1 + i + "." + "    ");
        for (int j = 0; j < sheet[i].length; j++) {
             double[][] voorlopig = null;
            voorlopig[i][j] = Double.parseDouble(sheet[i][j]); //<-- problem
            double d = voorlopig[i][j];
           // String s = " " + sheet[i][j];

            System.out.println(voorlopig);
            double thaprez = (precision / precision) + (Math.pow(precision, 10)) - 1;
            d = Math.floor(d * thaprez + .5) / thaprez;
            String s = " " + voorlopig[i][j];

            if (sheet[i][j] == null) {
                System.out.print(s + "\t\t");
            } else if (sheet[i][j].length() < 10) {
                System.out.print(s + "\t");
            } else {
                System.out.print(s);
            }
        }
        System.out.println("\n");
    }
}

Thanks in advance.

Hemang
  • 390
  • 3
  • 20
Brainshark
  • 5
  • 1
  • 3
  • `private final int ROWS; private final int COLUMNS; private final int WIDTH; //datavelden private String[][] sheet; private String[][] values; private int precision;` – Brainshark Dec 23 '15 at 09:58
  • 4
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Tom Dec 23 '15 at 10:00

2 Answers2

0

Try to initialize your array double[][] voorlopig maybe like this:

  double[][] voorlopig = new double[sheet.length][sheet.length];

You get NullPointerException because the array voorlopig was double[][] voorlopig = null;

Abdelhak
  • 8,299
  • 4
  • 22
  • 36
0
double[][] voorlopig = null;
voorlopig[i][j] = Double.parseDouble(sheet[i][j]);

voorlopig is set to null, you'll get a NullPointerException.Initialize voorlopig before using it.

Ramanlfc
  • 8,283
  • 1
  • 18
  • 24