有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java聚合中面临空指针异常

在学习Java聚合时,我遇到了NULLPointerException,请帮我找出根本原因。 有三节课

  1. 学生
  2. 部门
  3. 机构

    包裹通讯。JAVA遗产 导入java。util。列表 导入java。io.*

    public class Department {
    
        private String name;
        private List<Student> listofstudents;
    
        Department(String name,List<Student> listofstudents){
    
            this.name =name;
            this.listofstudents = listofstudents;
        }
    
        public List<Student> getStudents(){
    
            return this.listofstudents;
        }
    
    
    }
    

包裹通讯。JAVA继承

public class Student {

    private String name;
    private int id;
    private String Department;

    Student(String name,int id,String Department){

        this.name =name;
        this.id= id;
        this.Department = Department;

    }

}



    package com.java.inheritance;
    import java.util.List;

    public class Institute {

       private String institureName;
       private List<Department> listofDept;

      public Institute(String institureName,List<Department> listofDept){
           this.institureName = institureName;
           this.listofDept = listofDept;
       }

      public List<Department> getDepartments(){
          return this.listofDept;
      }

      // Get total number of Student in a Institute.

      public int gettotalnoofStudents(){
          int numberofStudent = 0;
          List<Student> listofStudent=null;
          for (Department dept:listofDept){
              listofStudent = dept.getStudents();
              for (Student student:listofStudent){

                  numberofStudent++;  
              }

          }

          return numberofStudent;
      }


    } // End of Institute

现在我在另一个班级叫他们MainClass

package com.java.inheritance;
import java.util.List;

public class MainClass {

    public static void main(String[] args) {

        try{  
        List<Student> sciencestudents=null;
        List<Student> artstudnets=null;
        Student s1= new Student("Asfakul",1,"Science");
        Student s2=new Student("Arizul",2,"Science");
        Student s3=new Student("Noor",3,"Arts");

        sciencestudents.add(s1);
        sciencestudents.add(s2);
        artstudnets.add(s3);


        Department d1=new Department("Science",sciencestudents);
        Department d2=new Department("Arts",artstudnets);

        List<Department> departmentlist=null;
        departmentlist.add(d1);
        departmentlist.add(d2);

        Institute i1= new Institute("BEC",departmentlist);
        System.out.println(i1.gettotalnoofStudents());
        }catch(Exception e){
            e.printStackTrace();    
        }
    }
}

但是现在我得到了空点异常。我哪里做错了


共 (1) 个答案

  1. # 1 楼答案

    在添加列表之前,需要先创建列表

        List<Student> sciencestudents=null;
        List<Student> artstudnets=null;
        Student s1= new Student("Asfakul",1,"Science");
        Student s2=new Student("Arizul",2,"Science");
        Student s3=new Student("Noor",3,"Arts");
    
        sciencestudents.add(s1);
        sciencestudents.add(s2);
        artstudnets.add(s3);
    

    在这里,您将添加到一个空列表,这将导致异常。尝试创建一个新的ArrayListLinkedList

    List<Student> sciencestudents = new ArrayList<>();
    List<Student> artstudents = new ArrayList<>();
    

    同样,你也需要创建一个部门列表

    List<Department> departmentlist = new ArrayList<>();