0

私はJavaとプログラミングに非常に慣れておらず、クラスプロジェクトに取り組んでいます.I / O、配列、オブジェクトについて学んでおり、従うべきかなり具体的なガイドラインがいくつかあります. コンパイラが「countryInfo[count].setName(name);」に到達したとき 、それは私に与えます

スレッド「メイン」での例外 java.lang.NullPointerException at Main.main(Main.java:53)

コメントアウトすると、次の行で同じエラーが発生します。このコードを書き直すもっと効率的な方法があると確信していますが、私たちは初心者なので、それを行うことは許可されていません。質問する前に Null 例外についてかなり読みました...どこかで見逃した場合はお詫びします。道に迷いました :(

public class Main {

/**
 * @param args the command line arguments
 */
private static Country[] countryInfo = new Country[43];

public static void main(String[] args) throws IOException {
    String name = "";
    String capital = "";
    String region = "";
    int region_Nbr = 0;
    int capital_population = 0;

    // TODO code application logic here
    String filename = "Countries.txt";
    String inputString;

    FileInputStream fis1 = new FileInputStream(filename);
    BufferedReader br1 = new BufferedReader(new InputStreamReader(fis1));
    inputString = br1.readLine();

    while (inputString != null) {
        int count = 0;
        System.out.print(inputString + "\n");

        name = inputString.substring(0, 13).trim();
        //System.out.print(name + ", "); //echo

        capital = inputString.substring(24, 36).trim();
        //System.out.print(capital + ", ");//echo

        region = inputString.substring(40, 56).trim();
        //System.out.print(region + ", "); //echo

        region_Nbr = Integer.parseInt(inputString.substring(64, 66).trim());
        //System.out.print(region_Nbr + ", ");//echo

        capital_population = Integer.parseInt(inputString.substring(72, inputString.length()).trim());
        //System.out.print(capital_population + "\n");

        countryInfo[count].setName(name);
        countryInfo[count].setCapital(capital);
        countryInfo[count].setRegion(region);
        countryInfo[count].setRegionNum(region_Nbr);
        countryInfo[count].setPopulation(capital_population);
        inputString = br1.readLine();

        count++;
    } //end while

}

}

public class Country {

private String name;
private String capital;
private String region;
private int region_Nbr;
private int capital_population;



public void setName(String w) {
    this.name = w;

} //end get name
4

2 に答える 2

3

配列をオブジェクトで埋めることはありません。現在、配列がありますが、それは空で、null だけで埋められています。解決策は、使用する前にオブジェクトを作成して埋めることです。配列は卵箱に似ていると考えてください。最初に卵で満たすまで、箱から卵を調理することはできません.

    countryInfo[count] = new Country(); // **** add
    countryInfo[count].setName(name);
    countryInfo[count].setCapital(capital);
    countryInfo[count].setRegion(region);
    countryInfo[count].setRegionNum(region_Nbr);
    countryInfo[count].setPopulation(capital_population);
    inputString = br1.readLine();

編集、あなたは次のように述べています:

なぜその行を追加する必要があるのか​​ 理解できません。private static Country[] countryInfo = new Country[43]; と思いました。

参照型の配列とも呼ばれる配列オブジェクトを作成すると、空の変数のコレクションが作成されます。これらの変数には、参照変数の既定値 null が与えられます。これは、配列をプリミティブのデフォルト (int の場合は 0、ブール値の場合は false など) に初期化するプリミティブ型の配列の作成とは異なります。

繰り返しますが、これは空の卵パックや駐車場を作ることに似ています。配列にオブジェクトを入れるには、自分でオブジェクトを配列に入れる必要があります。駐車場を車でいっぱいにしてから、車を選んで車を走らせる必要があります。空いている駐車場があるだけでは役に立ちません。

于 2013-05-10T02:02:56.787 に答える
0

次を追加する必要があります。

countryInfo[count] = new Country();

countryInfo[count].setName(name);
于 2013-05-10T02:10:31.123 に答える