我正在尝试将60个csv文件转换为多维数组,其值放在" 3D:基于[file] [row]和[cell]的位置。我刚开始使用java,我可能会忽略一些简单的东西,但一直试图解决这个问题。欢迎所有帮助。
错误:
Exception in thread "main" java.lang.NullPointerException
at GetDataFromCSV.test(GetDataFromCSV.java:30)
at GetDataFromCSV.main(GetDataFromCSV.java:14)
代码:
1 import java.io.BufferedReader;
2 import java.io.File;
3 import java.io.FileInputStream;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6
7
8 public class GetDataFromCSV {
9
10 public static void main(String[] args) throws IOException{
11 File dir = new File("C:\\Users\\PC\\workspace\\Administratie2\\csv");
12 File[] directoryListing = dir.listFiles();
13 String[][][] fileLineCell = new String[directoryListing.length][][];
14 test(fileLineCell, directoryListing);
15 System.out.println (fileLineCell[0][0][0]);
16 }
17
18 public static void test(String[][][] fileLineCell, File[] directoryListing) throws IOException{
19
20 for (int file = 0; file < directoryListing.length; file++) {
21 FileInputStream fstream = new FileInputStream(directoryListing[file]);
22 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
23 String strLine;
24
25 //Read File Line By Line
26 int line = 0;
27 while ((strLine = br.readLine()) != null){
28 String[] splitted = strLine.split(",");
29 for(int cell = 0; cell < splitted.length; cell++){
30 fileLineCell[file][line][cell] = splitted[cell];
31 line++;
32 }
33
34
35 }
36
37 //Close the input stream
38 br.close();
39
40
41 }
42
43
44
45 }
46 }
47
48
答案 0 :(得分:1)
String[][][] fileLineCell = new String[directoryListing.length][][];
此行仅初始化数组的第一个维度。所以这个3d数组中的每个对象实际上都是一个空的2d String数组(即每个0&lt; = index&lt; directoryListing.length)的fileLineCell [index] == null 你需要确定第二和第三角度的大小。如果您不了解它们,则应使用其他容器(如ArrayList)