我正在制作一个项目,我需要在二维数组中对值赋予一定的精度(0.00或0.000)。 2d数组是String
,我想将其转换为double
。但是当我尝试时,我得到一个NullPointerException
,我不知道为什么。
之后我想再次将其转换为String
数组。
打印方法代码:
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");
}
}
提前致谢。
答案 0 :(得分:0)
尝试初始化数组double[][] voorlopig
,如下所示:
double[][] voorlopig = new double[sheet.length][sheet.length];
您得到NullPointerException
因为数组voorlopig
是double[][] voorlopig = null;
答案 1 :(得分:0)
double[][] voorlopig = null;
voorlopig[i][j] = Double.parseDouble(sheet[i][j]);
voorlopig
设置为null
,您将获得NullPointerException
。在使用之前初始化voorlopig
。