-1

csvファイルに行の配列を作成しようとしています。

csvの例:

12,13,14,15
13,14,15,16
11,12,13,14

ここで、配列に3つの文字列を含める必要があります。

私はこれを得た:

public static String[] postcodeRows;
public static void main(String[] arg) throws Exception
{
    //read the csv file
    BufferedReader CSVFile = new BufferedReader(new FileReader(
            "C:\\Users\\randy\\Documents\\postcode csv\\exports\\all-groups.csv"));

    //count where we are in the csv file
    int csvLine = 0; 
    postcodeRows[0] = CSVFile.readLine(); // Insert the first line.
    // The while checks to see if the data is null. If it is, we've hit the end of the file. If not, process the data

    while (postcodeRows[csvLine] != null)
    {
        csvLine++;
        postcodeRows[csvLine] = CSVFile.readLine();
    }
    // Close the file once all data has been read.
    CSVFile.close();

}

今私はこれを手に入れます:

Exception in thread "main" java.lang.NullPointerException
at postcodeCheckup.postcodePanel.main(postcodePanel.java:43)

なぜあり、NullPointerExceptionどうすればこれを防ぐことができますか?変数を作成することはできません。ループnullによってチェックされます。while

4

2 に答える 2

2

はい、それはnullである可能性があります、あなたは間違ったものを見ています。:)

配列postCodeRowsは初期化されていません。

http://mathbits.com/MathBits/Java/arrays/Initialize.htmを確認してください。

乾杯。

于 2012-09-22T18:41:41.190 に答える
1

配列を初期化する必要があります。あなたの場合、配列は動的である必要があるように見えます。

javaarraylistの使用を検討してください。

于 2012-09-22T18:41:36.537 に答える