有四个与我的项目互动的类。目的是使用枚举,并使用枚举中的随机值来生成对象。EthicalEngine类。我将从另一个名为ScenarioGenerator的类中调用该方法。
Person类继承自Character抽象类。
我不确定初始化方法是否正确。我的想法是初始化Person类中ScenarioGenerator的实例,并使用方法getRandomPerson()调用构造函数。
但是我坚持,当我调用该方法时,仍然保持NullPointerException。
这是我的Character课程的一部分:abstract class Character {
private int age;
private Gender gender;
private BodyType bodyType;
protected Profession profession;
protected AgeCategory ageCategory;
protected boolean isPregnant;
public enum Gender {
MALE, FEMALE, UNKNOWN;
}
public enum BodyType {
AVERAGE, ATHLETIC, OVERWEIGHT, UNSPECIFIED;
}
public enum Profession {
DOCTOR, CEO, CRIMINAL, HOMELESS, UNEMPLOYED, MUSICIAN, BOXER ,UNKNOWN, NONE;
}
public Character(int age, Profession profession, Gender gender, BodyType bodyType, boolean isPregnant) {
this.age = age;
this.profession = profession;
this.gender = gender;
this.bodyType = bodyType;
this.isPregnant = isPregnant;
}
我的Person类的一部分:Person(int age, Profession profession ,Gender gender, BodyType bodyType, boolean isPregnant) {
super(age, profession, gender, bodyType, isPregnant);
}
public Profession getProfession () { //use getter to generate a random value
//only adults have profession
if (getAge()<=16 || getAge()>68) {
return Profession.NONE;
} else {
return Profession.values()[new Random().nextInt(Profession.values().length)];
}
//other getters and setters
}
我的ScenarioGenerator类的一部分:
公共类ScenarioGenerator {private Person person;
private Animal animal;
private Scenario scenario;
private Random random = new Random();
private int passengerCountMinimum;
private int passengerCountMaximum;
private int pedestrianCountMininum;
private int pedestrianCountMaximum;
public ScenarioGenerator() {
random.setSeed(random.nextInt());
}
public ScenarioGenerator(long seed) {
this.random = new Random(seed);
}
public Person getRandomPerson() {
//need age, gender, bodyType, profession, pregnancy
Person people = new Person(person.getAge(), person.getProfession(), person.getGender(),person.getBodyType(), person.isPregnant());
return people;
}
我的EthicalEngine类的一部分:public class EthicalEngine {
public static void main(String[] args) throws Exception {
EthicalEngine ethicalEngine = new EthicalEngine();
ScenarioGenerator scenarioGenerator = new ScenarioGenerator();
scenarioGenerator.getRandomPerson();
}