如题,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

解决方案 »

  1.   

    data[][]初始化了,这里只是一部分代码而已
      

  2.   

    你看报的错,到底是哪一行啊??double[][] data = new double[rows][columns];
    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]);
    }
    }
    }
      

  3.   

    NullPointerException就应该是哪边为空,stringData初始化没
      

  4.   

    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;
    }
    }
    代码是这样的,其中path是另外一个类传进来的,错误出现在data[i][j] = Double.parseDouble(stringData[i][j]);
    这一句
      

  5.   

    初始化为null,还是没有初始化啊
      

  6.   

    但是,可以把stringData[][]打印出来了啊,为什么进行转换的时候就又没有初始化了呢
      

  7.   


    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 
      

  8.   

    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();
    }
    }行和列对吗 ???
      

  9.   

    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();
    }
    }Cell ctemp = sheet.getCell();
      

  10.   

    data 没有初始化
    data = null 并没有初始化一个数组
    data = new double[rows][columns];
      

  11.   

    我打印出来的结果和excel表里面的格式是一样的
      

  12.   

    就是说data没有申请内存空间就进行赋值了。
      

  13.   

    数组初始化不能为null,要初始化长度