title: Class object array can get only one object
body: <p>So recently I've got into this problem, that every time I try to add two+ cars(trucks, busses or vehicles) program gets null pointer reference. Seems like my array can only hold one object. Why is that? Array size is set to 200... Adding one object works like a charm. This also works on C#. But not in Java.</p>
  
  <pre><code>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++;
      }
  </code></pre>
  
  <p>The code where I'm accesing the AddCar:</p>
  
  <pre><code>private static void Read(String text, Town[] towns) {
      String text1 = text;
      String dealership = null, adress = null, phone = null;
  
      ArrayList&lt;String&gt; line = new ArrayList&lt;&gt;();
      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 &lt; 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++;
          }
      }
  }
  </code></pre>