如题,String数组里面的内容都是数字,我用的方法是:
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
data[i][j] = Double.parseDouble(stringData[i][j]);
System.out.print(data[i][j]);
}
}
}
其中rows是数组的行数,columns是列数,但是报错NullPointerException
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
data[i][j] = Double.parseDouble(stringData[i][j]);
System.out.print(data[i][j]);
}
}
}
其中rows是数组的行数,columns是列数,但是报错NullPointerException
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
data[i][j] = Double.parseDouble(stringData[i][j]);
System.out.print(data[i][j]);
}
}
}
import jxl.Sheet;
import jxl.Workbook;public class JavaExcelApi {
double[][] data = null;
int rows = 0;
int columns = 0;
String[][] stringData = null; public String xlsImport(String path) {
Workbook book = null;
Sheet sheet = null;
try {
book = Workbook.getWorkbook(new File(path));
sheet = book.getSheet(0);
rows = sheet.getRows();
columns = sheet.getColumns();
stringData = new String[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
Cell ctemp = sheet.getCell(j, i);
stringData[i][j] = ctemp.getContents();
}
}System.out.println("数据读取成功,如下:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(stringData[i][j] + " ");
if(j == columns-1){
System.out.println();
}
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
data[i][j] = Double.parseDouble(stringData[i][j]);
System.out.print(data[i][j]);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return path;
} public int getRows() {
return rows;
} public int getColumns() {
return columns;
} public double[][] getData() {
return data;
}
}
代码是这样的,其中path是另外一个类传进来的,错误出现在data[i][j] = Double.parseDouble(stringData[i][j]);
这一句
import java.io.File;import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;public class JavaExcelApi {
double[][] data = null;
int rows = 0;
int columns = 0;
String[][] stringData = null; public String xlsImport(String path) {
Workbook book = null;
Sheet sheet = null;
try {
book = Workbook.getWorkbook(new File(path));
sheet = book.getSheet(0);
rows = sheet.getRows();
columns = sheet.getColumns();
stringData = new String[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
Cell ctemp = sheet.getCell(j, i);
stringData[i][j] = ctemp.getContents();
}
}System.out.println("数据读取成功,如下:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(stringData[i][j] + " ");
if(j == columns-1){
System.out.println();
}
}
}
// for (int i = 0; i < rows; i++) {
// for (int j = 0; j < columns; j++) {
// data[i][j] = Double.parseDouble(stringData[i][j]);
// System.out.print(data[i][j]);
// }
// }
} catch (Exception e) {
e.printStackTrace();
}
return path;
} public int getRows() {
return rows;
} public int getColumns() {
return columns;
} public double[][] getData() {
return data;
}
}
如果把转换这一段注释掉的时候,可以打开文件并且可以讲stringData打印出来:
数据读取成功,如下:
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
for (int j = 0; j < columns; j++) {
Cell ctemp = sheet.getCell(j, i);
stringData[i][j] = ctemp.getContents();
}
}行和列对吗 ???
for (int j = 0; j < columns; j++) {
Cell ctemp = sheet.getCell(j, i);
stringData[i][j] = ctemp.getContents();
}
}Cell ctemp = sheet.getCell(i, j);
data = null 并没有初始化一个数组
data = new double[rows][columns];