An Array of Objects (Class Initialization)
Hello, I'm new to the Java Forum :)
I've recently encountered a flaw in my programming skills... either I don't understand the concept of how to fill an array with objects, or I'm implementing the methods wrong. Anyway, I keep getting a "Null Pointer Exception" when I try to run a test class (which attempts to create a few "Person" objects and put them into an array of people). If anyone could help find the reason I'm getting this exception, I would greatly appreciate it! Smarty-pants Eclipse tells me that the exception occurs at line 23 of the Database class; my assumption is that I haven't correctly given the method to create a Person object with the data that is given for the method addPerson.
*Side-note: addPerson does a validity check on the person's gender (sex in my program's case). If it is not 'F' or 'M' it will return false, and will also return false if the array is full. This validity check also technically checks to see whether the person is actually added to the array or not (and should add the person to the people array if the returned Boolean is true.
*Side-note2: The Weight class isn't really important here. Everything is designed correctly with that class and so it only influences one field weight which I know is not the true cause of the Null Pointer Exception.
*Side-note3: The copyPerson method is meant to copy the skeleton of the people array and to fill it with Person objects to perform changes such as anonymizing data or performing queries to return specific values.
*Side-note4: Lots of notes... Most of the code here is all Setters and Getters for the various fields in the classes.
Here are the four classes:
Person Class:
Code:
public class Person {
private static String name;
private static int year;
private static double weight;
private static char sex;
public Person(String n, int y, double w, char s) {
name = n;
year = y;
weight = w;
sex = s;
}
public static void copyPerson() {
Database.getPeople();
Person[] otherPeople = new Person[Database.getPeople().length];
for (int i = 0; i < otherPeople.length; i++){
otherPeople[i] = Database.getPeople()[i];
i++;
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public static void report(boolean inMetric, Person[] people) {
if (inMetric = true) {
for (int i = 0; i < people.length; i++)
System.out.println(people[i]);
}
else {
copyPerson();
Weight.getStandard();
for (int i = 0; i < people.length; i++)
System.out.println(people[i]);
}
}
}
Database Class:
Code:
public class Database {
private static Person[] people;
private static int arrSize = 0;
public void main (String[] args) {
people = new Person[100];
}
public static Person[] getPeople() {
return people;
}
public void setPeople(Person[] people) {
this.people = people;
}
public static int getArrSize() {
return arrSize;
}
public void setArrSize(int arrSize) {
Database.arrSize = arrSize;
}
public boolean addPerson(String name, int year, double weight, char sex) {
int counter = 0;
people[counter] = new Person(name, year, weight, sex);
counter++;
if (sex != 'M' || sex != 'F')
return false;
else if (arrSize >= 100)
return false;
else
return true;
}
public String report(boolean inMetric) {
System.out.println("Report:\n");
Person.report(inMetric, people);
return arrSize + "people.\n";
}
}
Weight Class (not necessarily relevant to the problem, but included for reference just in case):
Code:
public class Weight {
private static double weight;
public static double getMetric() {
return weight;
}
public static double getStandard() {
weight = weight * 2.204623;
return weight;
}
public void setMetric(double weight) {
this.weight = weight;
}
public void setStandard(double weight) {
this.weight = weight;
}
}
Lastly, a simple Test Class:
Code:
public class TestProject {
public static void main(String[] args) {
Database db = new Database();
db.addPerson("Donn Sandmann", 1978, 50, 'M');
System.out.println(db.addPerson("Donn Sandmann", 1978, 50, 'M'));
db.addPerson("Hildegard Weatherby", 1988, 36, 'F');
db.addPerson("Shelli Steine", 1970, 45, 'F');
db.addPerson("Latrina Bubb", 1986, 47, 'F');
db.addPerson("Austin Mullan", 1990, 25, 'M');
db.addPerson("Bryant Wiren", 1995, 22, 'M');
}
}
Thanks in advance for help regarding the problem! I know that this task should be easy, but I suppose I don't fully understand the concepts surrounding it so I can't figure out how to hard-code it. Once someone provides me some example of how to actually add objects (e.g. Person objects) to an array of objects (e.g. Person[] people), I can figure this out :)