Java 如何修复ClassCastExecption和NullPointerException?

Java 如何修复ClassCastExecption和NullPointerException?,java,Java,我正在为我的OOP入门课程的一个项目写一些代码。该程序是根据用户输入的一些课程编写报告。我的测试课是这样的 import java.util.*; public class CourseReport { private ArrayList<Showable> courses = new ArrayList<Showable>(); ArrayList<Showable> students = new ArrayList<S

我正在为我的OOP入门课程的一个项目写一些代码。该程序是根据用户输入的一些课程编写报告。我的测试课是这样的

    import java.util.*;
    public class CourseReport {
    private ArrayList<Showable> courses = new ArrayList<Showable>();
    ArrayList<Showable> students = new ArrayList<Showable>();
    Scanner keyboard = new Scanner(System.in);
    public CourseReport() {
        char cont = 'y';
        while(cont == 'y') {
            courses.add(addCourse());
            System.out.println("Need to enter another course? (y.n)");
            cont = keyboard.next().charAt(0);
        }
    }
    public Showable addCourse() {
        System.out.println("Please enter the Course ID: ");
        int courseID = keyboard.nextInt();
        while((courseID < 200) || (courseID >= 300)) {
            System.out.println("Invaild course ID");
            courseID = keyboard.nextInt();
        }
        Showable instructor = addInstructor();
        char cont = 'y';
        while(cont == 'y') {
            students.add(addStudent());
            System.out.println("Need to enter another student? (y,n)");
            cont = keyboard.next().charAt(0);
        }
        Showable course = (Showable) new Course(courseID, instructor, 
        students);
        return course;
    }
    public Showable addStudent() {
        System.out.println("Please input the Student ID: ");
        int studentID = keyboard.nextInt();
        while (studentID < 0) {
            System.out.println("Invalid ID number, try again");
            studentID = keyboard.nextInt();
        }
        System.out.println("Please input the first name of the student");
        keyboard.nextLine();
        String firstName = keyboard.nextLine();
        System.out.println("Please input the last name of the student");
        String lastName = keyboard.nextLine();
        System.out.println("Please input the grade of the student");
        int grade = keyboard.nextInt();
        while ((grade > 100) || (grade <= 35)) {
            System.out.println("Invalid grade, try again");
            grade = keyboard.nextInt();
        }
        Showable student = new Student(firstName, lastName, studentID, grade);
        return student;
    }
    public Showable addInstructor() {
        System.out.println("Please input the first name of the professor");
        String firstName = keyboard.nextLine();
        System.out.println("Please input the last name of the professor");
        String lastName = keyboard.nextLine();
        System.out.println("Please input the statues of the professor");
        String statues = keyboard.nextLine();
        Showable instructor = new Instructor(firstName, lastName, statues);
        return instructor;
    }
    public void printReport() {
        Showable course;

        for(int i = 0; i < courses.size(); i++) {
            course = courses.get(i);
            course.show();
            ((Course) course).getInstructor().show();
            for(int j = 0; j < students.size(); j++) {
                ((Course) course).getStudents().get(j);
        }
        }
    }
    public static void main(String[] args) {
        CourseReport course = new CourseReport();
        course.printReport();
    }
    }
import java.util.*;
public class Course {
    private int courseID;
    private Showable instructor;
    private ArrayList<Showable> students = new ArrayList<Showable>();
    public int getCourseID() {
        return courseID;
    }
    public Showable getInstructor() {
        return instructor;
    }
    public void setCourseID(int courseID) {
        courseID = this.courseID;
    }
    public void setInstructor(Showable instructor) {
        instructor = this.instructor;
    }
    public void setStudents(ArrayList<Showable> students) {
        this.students = students;
    }
    public ArrayList<Showable> getStudents() {
        return students;
    }

    public Course() {
    }
    public Course(int courseID, Showable instructor, ArrayList<Showable> students) {
        courseID = this.courseID;
        instructor = this.instructor;
        students = this.students;
    }
    public String show() {
        return "courseID is: " + courseID;
    }
}
还有人知道为什么PrintReport方法中的内部for循环会抛出NullPointerExeception吗

public void printReport() {
        Showable course;

        for(int i = 0; i < courses.size(); i++) {
            course = courses.get(i);
            course.show();
            ((Course) course).getInstructor().show();
            for(int j = 0; j < students.size(); j++) {
                ((Course) course).getStudents().get(j);
        }
        }
    }
您的课程班级需要实施可展示的:

公共课课程实施可展示{ 在此之后,强制转换是不必要的。

如果您想将其强制转换为一个,则课程需要实现Showable。公共类课程实现Showable。完全删除强制转换也很好-您不需要它。对于NPE,请阅读