-1

While learning aggregation in Java, I have come across NULLPointerException , Please help me identifying the root cause. There are 3 classes.

  1. Student
  2. Department.
  3. Institution.

    package com.java.inheritance; import java.util.List; import 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;
        }
    
    
    }
    

package com.java.inheritance;

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

Now I am calling them in another class, 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();    
        }
    }
}

But I am getting NULLpointerException at this point. Where have I done wrong.

1 Answers1

2

You need to create your lists before you add to them.

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

Here you're adding to a null list, which causes the exception. Try creating a new ArrayList or LinkedList.

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

And similarly, you'll need to create a department list as well.

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