在我的Faculty类的线程主错误中获得异常

时间:2015-05-21 06:20:47

标签: java

    // 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() ;
    }
}

编写一个维护学生,课程和学习列表的驱动程序类  FacultyMembers并提供一个菜单,提供列出,创建和删除的方法  它们基于用户输入。驱动程序类也应该为a提供一种方法   学生添加和删除现有课程以及指派教师的方法   教授现有课程。当学生添加或时,不要创建新课程   当一名教师被指派教学时;让用户从中选择一个课程   列表。想一想在开始编码之前如何组织这个输入。

包含一个可以从main调用的方法,该方法将使用您的方法   添加和删​​除一些硬编码的测试数据(几个学生,几个教师   成员和几门课程。)这将让你编写列表并测试   不使用用户输入函数添加和删除项目的方法。

我的教师会员课程遇到困难我收到此错误

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)

1 个答案:

答案 0 :(得分:0)

如前所述,您在Person类的地址实例变量上调用toString,但是您从未实例化该变量,这意味着它具有Object的默认值:null。

如果您尝试调用非实例化变量的成员,它将抛出NullPointerException。

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

    }

删除地址的toString调用,或确保实例化它。

话虽如此,您的代码还有很多可以改进的地方:

像这样的代码:

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();
                }

效率不高。我想我们都同意,如果选择== 6,选择永远不会(同时)具有值7,8或9,但你仍然会测试它们。

改善这种情况的一种方法是使用else语句:

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();
                }

当你这样做时,它不会检查choice是否有其中一个值,如果它已经找到了它在之前测试中的值。因此,如果选择== 6,则不会测试7,8或9。

即使这样做好多了,但仍然不是很容易阅读。 switch语句可以在这里提供帮助:

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;
}

为此添加适当的缩进,您将更容易维护代码。

毫无疑问,你可以改进更多,但这会给你一些开始。