2D数组存储在另一个数组Java中

时间:2014-09-17 05:12:20

标签: arrays exception null 2d

您好我正在尝试将2D数组存储在另一个2D数组中,以便检查哪些变量不存在。(这对我来说很复杂)

然而,当我这样做时,我得到一个“空指针异常”

try {
    File file = new File(FILENAME);
    Scanner inputFile = new Scanner(file);

    // Read the number of Rows and Columns first
    numberOfRows = inputFile.nextInt();
    numberOfCols = inputFile.nextInt();
    data = new int[numberOfRows][numberOfCols];

    // Complete the reading of data from the file - see Lecture Notes
    for (int row = 0; row < numberOfRows; row++)
    {
        for (int col = 0; col < numberOfCols; col++)  
        {
            data[row][col] = inputFile.nextInt();

            check[row][col] = data[row][col];

        }
    } 

inputFile.close();
}
catch (FileNotFoundException ex) {
    System.out.println("Error reading data from " + FILENAME + " Exception = " + ex.getMessage());
  }

谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

当我们通过 new 关键字使用没有分配内存的任何变量时,会发生

NullPointerException 所以请再次检查你的代码,可能是2个变量值 numberOfRows,numberOfCols