Не добавляйте null
, , просто замените его на ""
:
Пример:
cars.add(new Car(2000,"","caravan"));
cars.add(new Car(2010,"dodge",""));
Код:
List<Car> cars = new ArrayList<Car>();
cars.add(new Car(2016,"honda","civic"));
cars.add(new Car(2017,"Lamborghini","aventador"));
cars.add(new Car(2000,"","caravan"));
cars.add(new Car(2010,"dodge",""));
for(Car c : cars) {
System.out.print(c.getCompanyName() + "" + c.getCarName());
}
В качестве альтернативы обновите код для пропуска значений null
:
for(Car c : cars) {
String data = ((c.getCompanyName() != null) ? c.getCompanyName(): "") + ((c.getCarName() != null) ? c.getCarName() : "");
System.out.print(data);
}