2016-10-19 22 views
0

60個のcsvファイルを、[ファイル] [行]と[セル]に基づいて「3D:位置」に配置された値を持つ多次元配列に変換しようとしています。 Javaは、私はsimpel何かを見下ろすことかもしれないが、日のためにこれを修正しようとしているとすべてのヘルプは大歓迎です複数のCSVファイルをJavaの3D配列に変換する

エラー:。。

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

String [] [] [] fileLineCell = new String [directoryListing.length] [] []; 正しく作成できませんでした – user2717954

答えて

1
String[][][] fileLineCell = new String[directoryListing.length][][]; 

この行は、配列の最初の次元のみを初期化します。したがって、この3次元配列内のすべてのオブジェクトは、実際にはnullの2次元配列です(つまり、0の場合はfileLineCell [index] == null)。 = index < directoryListing.length) 2番目と3番目の次元のサイズを修正する必要があります。あなたがそれらを知らないなら、他のコンテナ(ArrayListのような)を使うべきです。

関連する問題