2016-12-11 5 views
0

最近、私は2つの+車(トラック、バスまたは車両)を追加しようとするたびにnullポインタ参照を取得するというこの問題に遭遇しました。私の配列は1つのオブジェクトしか保持できないようです。何故ですか?配列のサイズは200に設定されています... 1つのオブジェクトを追加すると、魅力のように動作します。これはC#でも動作します。しかし、Javaではありません。クラスオブジェクトの配列は1つのオブジェクトしか取得できません

public class Town { 

    public int MaxNumberOfCars = 200; 
    public String Dealership; 
    public String Adress; 
    public String Phone; 
    public Car[] Cars = new Car[MaxNumberOfCars]; 
    public Bus[] Busses = new Bus[MaxNumberOfCars]; 
    public Truck [] Trucks = new Truck[MaxNumberOfCars]; 
    public Vehicles[] Vehicles = new Vehicles[MaxNumberOfCars]; 
    public static int carCount; 
    public static int busCount; 
    public static int truckCount; 
    public static int vehicleCount; 
    public int townVehicleCount; 
    public int DealershipCount; 
    public double avgage; 


    public Town(String dealership, String adress, String phone) { 
     Dealership = dealership; 
     Adress = adress; 
     Phone = phone; 
    } 

    public void AddCar(Car car) { 
     Cars[carCount++] = car; 
     vehicleCount++; 
    } 

私はAddCar accesingてるコード:あなたのカウント変数は静的ですなぜ

private static void Read(String text, Town[] towns) { 
    String text1 = text; 
    String dealership = null, adress = null, phone = null; 

    ArrayList<String> line = new ArrayList<>(); 
    StringTokenizer st = new StringTokenizer(text1, "\n"); 
    int count = st.countTokens()-3; 
    if (line != null) { 
     dealership = st.nextToken(); 
     adress = st.nextToken(); 
     phone = st.nextToken(); 

     towns[townCount] = new Town(dealership, adress, phone); 

     for(int i = 0; i < count; i++) { 

      String string = st.nextToken(); 
      String[] values = string.split(";"); 
      String licenseplates = values[0]; // 004 
      char type = values[1].charAt(0); 
      String brand = values[2]; 
      String model = values[3]; 
      YearMonth yearofmake = YearMonth.parse(values[4]); 
      YearMonth techinspection = YearMonth.parse(values[5]); 
      String fuel = values[6]; 
      int fuelconsumption = Integer.valueOf(values[7]); 
      switch (type) { 
       case 'c': 
        Car car = new Car(licenseplates, brand, model, yearofmake, techinspection, fuel, fuelconsumption); 
        towns[townCount].AddCar(car); 
        towns[townCount].AddVehicle(car); 
        break; 
      } 
      townCount++; 
     } 
    } 
} 
+1

あなたは 'Town'オブジェクトを使用しているコードを投稿します。 – GurV

+0

@グルービンガーシンガー。 –

+0

私は@ GurwinderSinghに同意します。私はちょうど 'main()'テストでマシン上のコードを実行し、コンパイルして実行しました。あなたのクライアントコードが壊れていると思われます。エラーを投げているメソッドを追加してください。また、必要な場合は、MVCE(http://stackoverflow.com/help/mcve)を利用できるように、可能な限り減らしてください。 – entpnerd

答えて

0

あなたの問題は、配列townsに十分な町がなくてもtownCountを増やしていることです。配列に町を追加するか、forループの最後にtownCount++;行を削除する必要があります。

+0

ありがとうございます! –

+0

ようこそ。それが助けてくれてうれしい。 – entpnerd

0

を? まずこれを変更する必要があると思います。 addCarメソッドでMaxNumberOfCars検証をチェックするような検証を追加する必要があります。

+0

MaxNumberOfCarsを超えた場合、ArrayIndexOfBoundExceptionが返されます。 –

+0

カウント変数を静的にしました。何も変わっていません。 –

+0

Townオブジェクトを使用している場所にコードを投稿します。 –

関連する問題