Wilson  Wilson - 2 months ago 9
Java Question

Getting an Exception in thread main error on my Faculty Class

// to create address object, and pass the address to faculty
// Must connect to other classes simultaneously

import java.util.ArrayList;

import javax.swing.JOptionPane;

public class Driver {
public static ArrayList<Student> student = new ArrayList<Student>();
public static ArrayList<Course> course = new ArrayList<Course>();
public static ArrayList<FacultyMember> faculty = new ArrayList<FacultyMember>();

public static void main(String[] args) {
String[] choices = { "0 Exit", "1 Add Student", "2 List of Students",
"3 Remove Student", "4 Create course", "5 List of Courses",
"6 Remove Course", "7 add instructor", "8 List of Instructors",
"9 Remove Instructor" };

student.add(new Student(308765433, "phillip", 2436, "Daly Street",
"Los Angeles", "CA", "USA"));
student.add(new Student(308765434, "Wilson Lucey", 2436,
"Broadway Street", "Los Angeles", "CA", "USA"));
student.add(new Student(308765432, "William", 2436,
"University Street", "Los Angeles", "CA", "USA"));

course.add(new Course(23545, "CS201"));
course.add(new Course(23546, "CS202"));
course.add(new Course(23547, "CS203"));

faculty.add(new FacultyMember(23477, "John"));
// faculty.add(new FacultyMember(23587, "Keenan"));
// faculty.add(new FacultyMember(236, "Parviz"));

int a = -1;
while (a < 0) {
int choice = JOptionPane.showOptionDialog(null, "Welcome", "Menu",
a, a, null, choices, a);
if (choice == 0) {
System.exit(0);
}
if (choice == 1) { // DONE
addStudent();

}
if (choice == 2) { // List all students inside array of students.
String a1 = "";
for (Student b : student) {
// b.toString();
a1 = a1 + b.toString() + "\n";

}
JOptionPane.showMessageDialog(null, a1);
}

if (choice == 3) {
removeStudent();
}
if (choice == 4) { // Show all courses inside array of courses
addCourse();
}
if (choice == 5) { // List of Course
String c1 = "";
for (Course b : course) {
// b.toString();
c1 = c1 + b.toString() + "\n";
}
JOptionPane.showMessageDialog(null, c1);
}
if (choice == 6) {
removeCourse();

}
if (choice == 7) {
addFaculty();
}
if (choice == 8) { // LIST OF FACULTY
String d1 = "";
for (FacultyMember d : faculty) {
// b.toString();
d1 = d1 + d.toString() + "\n";

}
JOptionPane.showMessageDialog(null, d1);
}
if (choice == 9) { // REMOVE FACULTY
removeFaculty();
}
}
}

private static void addStudent() {
int CIN = Integer.parseInt(JOptionPane.showInputDialog("Enter CIN: "));
String Name = JOptionPane.showInputDialog("Enter name");
// int street, String name, String city, String state, String country
int Street = Integer.parseInt(JOptionPane
.showInputDialog("Enter street #"));
String StreetName = JOptionPane.showInputDialog("Enter Street Name");
String City = JOptionPane.showInputDialog("Enter city");
String State = JOptionPane.showInputDialog("Enter state");
String Country = JOptionPane.showInputDialog("Enter country");

Student s1 = new Student(CIN, Name, Street, StreetName, City, State,
Country);

student.add(s1);
}

private static void addCourse() { // If

int courseID = Integer.parseInt(JOptionPane
.showInputDialog("Enter the id numer please!"));
String courseTitle = JOptionPane
.showInputDialog("Enter a course please!");
// String instructorName = JOptionPane
// .showInputDialog("Enter instructor for the course please!");
Course c1 = new Course(courseID, courseTitle); // CREATE OBJECT
course.add(c1);
}

// private static void Course(int courseID, String courseTitle, String term,
// String instructorName) {
//
// }

private static void addFaculty() {
int employeeID = Integer.parseInt(JOptionPane
.showInputDialog("Enter the employee id numer please!"));
String facultyName = JOptionPane
.showInputDialog("Enter the name of faculty please!"); // SAME
// AS
// STUDENTS
FacultyMember f1 = new FacultyMember(employeeID, facultyName);
faculty.add(f1);
}

public static void removeStudent() {

String a1 = "";
int i = 0;
for (Student b : student) {
// b.toString();
a1 = a1 + i++ + b.toString() + "\n";

}

int LineNumber = Integer.parseInt(JOptionPane.showInputDialog(null, a1
+ "Enter a row # to remove, please"));
student.remove(LineNumber);
for (int k = 0; k < student.size(); k++) {
System.out.println(student.get(k));

}
}

public static void removeCourse() {

String a1 = "";
int i = 0;
for (Course b : course) {
// b.toString();
a1 = a1 + i++ + b.toString() + "\n";
}

int LineNumber = Integer.parseInt(JOptionPane.showInputDialog(null, a1
+ "Enter a row # to remove, please"));
course.remove(LineNumber);
for (int k = 0; k < course.size(); k++) {
System.out.println(course.get(k));
}
}

public static void removeFaculty() {

String b1 = "";
int i = 0;
for (FacultyMember b : faculty) {
// b.toString();
b1 = b1 + i++ + b.toString() + "\n";

}
// JOptionPane.showMessageDialog(null,
// a1+"Enter a row # to remove, please");
//

int LineNumber = Integer.parseInt(JOptionPane.showInputDialog(null, b1
+ "Enter a row # to remove, please"));
faculty.remove(LineNumber);
for (int k = 0; k < faculty.size(); k++) {
System.out.println(faculty.get(k));

}
}
}
// }

public class Address {

/*
* An address has a street number, street name, city, state or province, and
* country.
*/
private int street;
private String name;
private String city;
private String state;
private String country;

public Address(int street, String name, String city, String state, String country) {
this.street = street;
this.name = name;
this.city= city;
this.state = state;
this.country = country;
}

public int getStreet() {
return street;
}

public void setStreet(int street) {
this.street = street;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCity(String city){
return city;
}

public void setCity(String city){
this.city=city;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public String toString() {
return name + " "+
"Street: "+street+ " "+
"City: "+city+ " , "+ " State: "+ state +
"Country: "+country;
}
}

public class Person {

/*
* A Person has a name and an Address (represented by an object of class
* Address, not a String). Note that the subclasses of Person inherit the
* fields and methods of Person. You may need to override some of the
* methods in the subclasses.
*/

protected String name;
protected Address address;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public void setAddress(int street, String name, String city, String state,
String country) {

Address set1 = new Address(street, name, city, state, country);
this.address = set1;
}

public String toString() {
return "Name: " + name + " "+"Address: " + address.toString();
}
}


public class Course {

private int identifier;
private String courseTitle;

public Course(int identifier, String courseTitle){
this.identifier= identifier;
this.courseTitle= courseTitle;

}

public int getIdentifier() {
return identifier;
}

public String getCourseTitle() {
return courseTitle;
}

public String toString(){
return "Course ID: "+identifier + "Course title: "+ courseTitle;
}

public boolean contains(Course course) {
// TODO Auto-generated method stub
return false;
}


//setter to change the object values
//getters is to return some values we need


}

import java.util.ArrayList;
import java.util.List;

public class FacultyMember extends Person{
private int employeeID;
public ArrayList<Course>course = new ArrayList<Course>();

public FacultyMember(int employeeID, String nameIn){
this.name= nameIn;
this.employeeID = employeeID;
}
public int getID(){
return employeeID;
}
public void setID(int employeeID){
this.employeeID = employeeID;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public List<Course> getCourse(){
return course;

}
public void addCourse(Course course) {
this.course.add(course);
}

public String toString(){
return "EmployeeID: " + employeeID +super.toString() ;
}
}


Write a Driver class that maintains lists of Students, Courses, and
FacultyMembers and has a menu that provides ways to list, create, and delete
them based on user input. The driver class should also provide a way for a
student to add and drop existing Courses and a way to assign faculty member
to teach existing Courses. Do not create a new Course when a Student adds or
when a faculty member is assigned to teach; let the user choose a Course from
the list. Think about how to organize this input before you start coding.

Include a method that can be called from main that will use your methods to
add and delete some hard-coded test data (several students, several faculty
members, and several courses.) This will let you code the lists and test the
methods to add and delete items without using the user input functions.

I am having difficulty with my Faculty Member class I am getting this error

Exception in thread "main" java.lang.NullPointerException
at Person.toString(Person.java:30)
at FacultyMember.toString(FacultyMember.java:33)
at Driver.main(Driver.java:83)

Answer

As said earlier, you are calling toString on the address instance variable in your Person class, but you never instantiate the variable, meaning it has the default value for an Object: null.

If you try to call a member of a non instantiated variable, it will throw a NullPointerException.

public String toString() {
        return "Name: " + name + " "+"Address: " + address.toString();

    }

Either remove the toString call for address, or make sure you instantiate it.

That being said, there is much that can be improved on your code:

code like this:

if (choice == 6) {
                    removeCourse();

                }
                if (choice == 7) {
                    addFaculty();
                }
                if (choice == 8) { // LIST OF FACULTY
                    String d1 = "";
                    for (FacultyMember d : faculty) {
                        // b.toString();
                        d1 = d1 + d.toString() + "\n";

                    }
                    JOptionPane.showMessageDialog(null, d1);
                }
                if (choice == 9) { // REMOVE FACULTY
                    removeFaculty();
                }

is not efficiƫnt. I think we can both agree, that if choice == 6, choice will never (at the same time) have the value7, 8 or 9, yet you still test for them.

One way to improve this, is using else statements:

if (choice == 6) {
                    removeCourse();

                }else if (choice == 7) {
                    addFaculty();
                } else if (choice == 8) { // LIST OF FACULTY
                    String d1 = "";
                    for (FacultyMember d : faculty) {
                        // b.toString();
                        d1 = d1 + d.toString() + "\n";

                    }
                    JOptionPane.showMessageDialog(null, d1);
                }else if (choice == 9) { // REMOVE FACULTY
                    removeFaculty();
                }

When you do this, it will not check if choice has one of the values, if it already found the value it has in a previous test. So, if choice == 6, it will not test for 7, 8 or 9.

Even though this is a lot better, it 's still not very easy to read. a switch statement can help here:

switch(choice){
case 6: removeCourse();
        break;
case 7: addFaculty();
        break;
case 8: String d1 = "";
        for (FacultyMember d : faculty) {
        // b.toString();
        d1 = d1 + d.toString() + "\n";}
        JOptionPane.showMessageDialog(null, d1);
        break;
case 9: removeFaculty();
        break;
}

Add proper indentation to that, and you'll have a lot easier to maintain code.

No doubt there is more you can improve, but this 'll give you something to start with.