For this i am running BlueJ doing object-oriented java and i have JVM 7.1 active….
Ok so i have to make an application for my degree and im having trouble. I am thankful the “teacher” has let me use my tri-array system since the iterator messed up my single-array system.
Regrettably, i was unable to find anything on the internet to help with my issue…
Basically when i go to add a “Student” to the array, the object is null in all fields. and i cannot figure out how to modify them.
My arrays use the diamond notation. Similar to the following:
private ArrayList<Student> students;
this is the form of array i am currently using and there are 2 more arrays.
private ArrayList<CollegeEmployee> staff;
private ArrayList<Faculty> members;
I was advised to use a single array but as said, the iterator messed it up and as long as i can fix it, i dont care how many arrays i have lol.
The program runs within a while loop and prompts the user to enter a letter for a certain command. when the user enters “s” a student is then added to the array and this is where the problem comes in. the object is given null values and i have no clue how to modify them…the following is the method that calls the object constructor
private void addStudent()
{
Student b = new Student(firstName, lastName, address, postCode, phoneNumber, gradePointAvg, selectedCourse);
students.add(b);
}
When the array is printed as the user quits the program, i get the following input from the terminal window:
null, null, null, 0, 0
0
null
My question is: how do i give object b values to override the null ones. Btw the values must be entered by the user and not pre-set in the declaration of the array.
Thank you for your time 🙂
This stuff makes me hate code xD
EDIT:
Here is the constructor and fields from the student class:
private String title;
public double gradePointAvg;
public String selectedCourse;
/**
* Constructor for objects of class Student
*/
public Student(String firstName, String lastName, String address, int postCode, int phoneNumber, double gradePointAvg, String selectedCourse)
{
super(firstName, lastName, address, postCode, phoneNumber);
this.title = title;
this.gradePointAvg = gradePointAvg;
this.selectedCourse = selectedCourse;
}
Person superclass constructor:
public String firstName;
public String lastName;
public String address;
public int postCode;
public int phoneNumber;
/**
* Constructor for objects of class Person
*/
public Person(String firstName, String lastName, String address, int postCode, int phoneNumber)
{
// initialise fields
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.postCode = postCode;
this.phoneNumber = phoneNumber;
}
The method above, addStudent calls the student constructor which then calls the person constructor. Yes it does have to be like this as its the restrictions of the project.
And i made my fields public for now since they were set as protected because private is local scope.
Thank you guys so much for checking my code out..i wish my teacher was better >.<
In your Student class
firstName, lastName, address,selectedCourseare presumablyStringand Java will initialize instance variables which are Objects (String in your case) tonull, for your other fields namelypostCode, phoneNumberwhich I think areintdefault value is 0.When you are in
addStudentmethod check the values that you are using to construct the object, it seems all Strings are null and int are not initialized.When above code is executed I think none of the parameters like firstName, lastName e.t.c have any value, thats why it is showing you null.
Before you add new
Studentobject to your liststudents, prompt your user to give values forfirstName, lastName, addresse.t.c, then use those values to construct yourStudentobject.Update Sample Code:
Student Class:
The main class