BonusEmporium
의 내 메서드 applyBonus()
은 City
개체의 arrayList에서 보너스 개체를 가져오고 Player
개체에 적용합니다. 나는이처럼 내 함수를 작성할 때 내가 아무 문제를 테스트가 없습니다 :배열 요소가 비어 있는지 확인
public class BonusEmporium extends Bonus {
public BonusEmporium(int cities) {
this.setBonusType(BonusType.EMPORIUM);
this.cities=cities;
setCity(new ArrayList<City>(cities));
}
public void applyBonus(Player player){
Bonus bonus=getCity().get(0).getBonus().get(0);//gets the first bonus from the first
//city
bonus.applyBonus(player);
Bonus bonus=getCity().get(0).getBonus().get(1);//gets the second bonus from the first
//city
bonus.applyBonus(player);
Bonus bonus=getCity().get(1).getBonus().get(0);//gets the first bonus from the
//second city
bonus.applyBonus(player);
}
}
내가 arraylists 내가 배열에서 요소가 비어 있는지 확인 어떻게, 요소를 포함하는 동안 만 실행 할 때 문제가 ?
public class BonusEmporium extends Bonus {
public BonusEmporium(int cities) {
this.setBonusType(BonusType.EMPORIUM);
this.cities=cities;
setCity(new ArrayList<City>(cities));
}
public void applyBonus(Player player){
int i=0,j=0;
while(j<cities){
while(getCity().get(j).getBonus().get(i)!=null){//In theory this should
//verify that the element bonus i from the city j is not empty
//but i get NullPointerException
Bonus bonus=getCity().get(j).getBonus().get(i);
bonus.applyBonus(player);
i++;
}
j++;
}
}
}
확인하지 않고 괜찮을에 널 (null)을 넣지 마십시오 루프 (크기를 지정하지) 생성자의 모든 ArrayLists을 초기화하기 null이 비어 있지 않다는 의미입니다. –
null (NULL 이외의 null)을 의미 할 수 있습니다. null은 null이 아니지만 더 철학적 인 수준 일 수 있습니다. –
@PavelNiedoba null은 할당되지 않았거나 참조되지 않았 음을 의미합니다. List 객체 자체가 null이 아니면 길이가 0입니다. –