构造有向图,获得空指针异常

时间:2015-11-19 11:04:14

标签: java

我以邻接列表的形式构建有向图。当我尝试打印每个课程预先要求时,我正在接收NullPointerException,并且我不确定原因。它与Course类是静态的有关吗?

   public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner in = new Scanner(System.in);
            int numCourses = Integer.parseInt(in.nextLine());

            Course[] adjList = new Course[numCourses];

            //Create our courses
            for (int i = 0; i < numCourses; i++){
                    Course c = new Course(i);
                    adjList[i] = c;
            }

            //input we are reading in, each line ends with a 0.
            //7       -> number of courses
            //3 0     -> Course 1 has 1 prereq, course 3
            //0       -> Course 2 has no prereqs
            //2 0     -> Course 3 has 1 Pre-req, course 2
            //1 2 6 0 -> Course 4 has 3 prereqs, courses 1, 2, 6
            //3 1 0   -> Course 5 has 2 prereqs, courses 3 and 1
            //4 0     -> Course 6 has one prereq, course 4
            //0       -> Course 7 has no prereqs
            for (int i = 0; i <numCourses; i++){
                    String input = in.nextLine();
                    String[] split = input.split("\\s+");
                    for (int j = 0; j < split.length; j++){
                            adjList[i].setPreReqSize(split.length-1); //set number of preReqs, -1 because of the ending 0 we dont want to include
                            if (Integer.parseInt(split[j])== 0){
                                    break;
                            }
                            else{
                                    adjList[i].preReqs[j] = adjList[Integer.parseInt(split[j])];
                            }
                    }
            }

            for (Course c: adjList){
                    c.printPreReqs();
            }

    }

    public static class Course{
            int courseNum;
            boolean visited = false;
            Course[] preReqs;

            public Course(int num){
                    this.courseNum = num;
            }

            public void setPreReqSize(int n){
                    this.preReqs = new Course[n];
            }

            public String printPreReqs(){
                    if (this.preReqs.length == 0){
                            return "None";
                    }
                    else{
                            String result = "Course"+courseNum+ " has the following preReqs ->";
                            for (int i = 0; i < this.preReqs.length; i++){
                                    result += " Course" + this.preReqs[i].courseNum + " ";
                            }
                            return result;
                    }
            }
    }

}

2 个答案:

答案 0 :(得分:1)

有两个问题:

1)异常是因为您继续为preReqs中的每个字符串创建新数组split。要修复,请将adjList[i].setPreReqSize(split.length-1);移出内部for循环(上面一行应该这样做)。

2)一个小问题:数组索引从0开始,但您的courseNum似乎从1开始。你需要照顾它。

以下更改应修复它:

Course c = new Course(i+1);

adjList[i].preReqs[j] = adjList[Integer.parseInt(split[j]) - 1];

答案 1 :(得分:1)

我建议将此功能更改为更具防御性:

        public String printPreReqs(){
                if ( (this.preReqs==null) || (this.preReqs.length == 0)){
                        return "None";
                }
                else{
                        String result = "Course"+courseNum+ " has the following preReqs ->";
                        for (int i = 0; i < this.preReqs.length; i++){
                                result += " Course" + this.preReqs[i].courseNum + " ";
                        }
                        return result;
                }
        }

这样可以防止在preReqs中初始化setPreReqSize时出现一些极端情况,这至少不能完全确定。

顺便说一句:你在哪个时候准确得到了NullPointerException