So i dont know why i am getting this error, here are my 3 classes plus the tester

edit: I get the null exception after i run the program,
--------------------Configuration: <Default>--------------------
Exception in thread "main" java.lang.NullPointerException
at University.offerCourse(University.java:35)
at StudentCourseTestProgram.main(StudentCourseTestProgram.java:53)

Process completed.

import java.util.*;

public class Student {

	private String name;
	private int year, studentNumber;

	ArrayList<Course> courses;


    public Student(String n, int s, int y) {
    	name = n;
    	studentNumber = s;
    	year = y;

    }

    public Student(){

		name = "";
		studentNumber = 0;
		year = 2012;
		courses = new ArrayList<Course>();

	}

	public String getName(){
		return name;
	}

	public int getStudentNumber(){
		return studentNumber;
	}

    public int getYear(){
    	return year;
    }

    public ArrayList getCourses(){
    	return courses;
    }

    public void addACourse(Course c){
    	courses.add(c);
    }

    public void removeACourse(Course c){
    	courses.remove(c);
    }

    public ArrayList<Student> classmatesAt(University u){
    	ArrayList<Student> studentCopy = new ArrayList<Student>();
		ArrayList<Student> classmates = new ArrayList<Student>();


		while (!studentCopy.isEmpty()){
			Student s = studentCopy.get(0);
			ArrayList g = s.getCourses();
			classmates.add(s);
				Iterator i = studentCopy.iterator();
			while(i.hasNext()){
				Student n = (Student)i.next();
				if(n.getCourses().equals(g)){
					i.remove();
				}
			}
		}

		return classmates;


 	}

    public String toString(){
    	return (name + " " + studentNumber + " " + year);
    }

}

/**
 * @(#)Course.java
 *
 *
 * @author
 * @version 1.00 2012/2/28
 */
import java.util.*;

public class Course {

	private String title, code;
	private int number;

	ArrayList<Student> students;


    public Course(String c, int n , String t) {
    	code = c;
    	number = n;
    	title = t;

    }

    public Course(){
    	code = " ";
    	number = 0;
    	title = " ";
    	students = new ArrayList<Student>();

    }

    public String getCode(){
    	return code;
    }

    public int getNumber(){
    	return number;
    }

    public String getTitle(){
    	return title;
    }

    public ArrayList getStudents(){
    	return students;
    }

    public void addStudent(Student s){
    	students.add(s);
    }

    public void removeStudent(Student s){
    	students.remove(s);
    }

     public String toString(){
    	return (code + number + "---" +title);
    }


}


/**
 * @(#)University.java
 *
 *
 * @author
 * @version 1.00 2012/2/28
 */
 import java.util.*;


public class University {

	private String name;
	private ArrayList<Course> courses;
	private ArrayList<Student> students;

    public University(String n) {
    	name = n;
    }

    public String getName(){
    	return name;
    }

    public ArrayList getCourses(){
    	return courses;
    }

    public ArrayList getStudents(){
    	return students;
    }


    public void offerCourse(Course c){
    	courses.add(c);
    }

    public void cancelCourse(Course c){
    	//check to see if course is empty
    	courses.remove(c);
    }

    public void enrollStudentInCourse(Student s, Course c){
    	//check to see if course exist
    	students.add(s);
    	courses.add(c);
    }

    public void withdrawStudentFromCourse(Student s, Course c){
    	//check to see if student is enrolled in course
    	students.remove(s);
    	courses.remove(c);
    }

    public ArrayList<Course> lowRegistrationCourses(int min){
		ArrayList<Course> lowReg = new ArrayList<Course>();


		for(Course c: courses){
			if((students.size())== min || (students.size()) < min )
				lowReg.add(c);
		}
		return lowReg;
    }

    public Course highestEnrollment(){
		int highestEnrollment = (courses.size());
		Course h = courses.get(0);
		for(Course c: courses){
			if(highestEnrollment < courses.size())
			{
				highestEnrollment = courses.size();
				h = c;
			}
		}
		return h;

	}

	public Student busiestStudent(){
		int busiestStudent = (students.get(0)).courses.size();
		Student s = students.get(0);
		for(Student c: students){
			if(busiestStudent < courses.size())
			{
				busiestStudent = courses.size();
				s = c;
			}
		}
		return 	s;

	}


    public String toString(){
    	return (name + ":" + courses.size() +" Courses, " + students.size() + " Students");
    }
}


Tester
/**
 * @(#)StudentCourseTestProgram.java
 *
 *
 * @author
 * @version 1.00 2012/2/28
 */


public class StudentCourseTestProgram {
	public static void main(String args[]) {
		Student s1 = new Student("Jackie Chen", 100123456, 2005);
		Student s2 = new Student("Bruce Willis", 100234567, 2007);
		Student s3 = new Student("Keanu Reeves", 100345678, 2007);
		Student s4 = new Student("Carrie Anne Moss", 100456789, 2005);
		Student s5 = new Student("Leonardo Dicaprio", 100567890, 2006);
		Student s6 = new Student("Cate Blanchett", 100678901, 2007);
		Student s7 = new Student("Clint Eastwood", 100789012, 2006);
		Student s8 = new Student("Hilary Swank", 100890123, 2007);
		Student s9 = new Student("Matt Damon", 100901234, 2001);
		Student s10 = new Student("Jack Nicholson", 100000001, 2007);

		Course c1 = new Course("BIOL", 1003, "Introductory Biology I");
		Course c2 = new Course("BIOL", 2303, "Microbiology");
		Course c3 = new Course("BIOL", 3201, "Cell Biology");
		Course c4 = new Course("BIOL", 4209, "Advanced Plant Physiology");
		Course c5 = new Course("ECE", 1005, "Introduction to Object-Oriented Programming");
		Course c6 = new Course("ECE", 2003, "Computer Organization");
		Course c7 = new Course("ECE", 3008, "User Interface Architecture");
		Course c8 = new Course("ECE", 4002, "Computer Graphics");
		Course c9 = new Course("ECE", 4106, "Artificial Intelligence");
		Course c10 = new Course("ECE", 4807, "Mobile Robot Programming");
		Course c11 = new Course("MGMT", 1001, "Principles of Financial Accounting");
		Course c12 = new Course("MGMT", 2204, "Basic Marketing");
		Course c13 = new Course("MGMT", 2601, "Business Law");
		Course c14 = new Course("MGMT", 3304, "Forecasting Methods in Business");
		Course c15 = new Course("MGMT", 4205, "International Marketing");
		Course c16 = new Course("CHM", 1000, "General Chemistry");
		Course c17 = new Course("CHM", 2203, "Organic Chemistry I");
		Course c18 = new Course("CHM", 3107, "Experimental Methods in Nanoscience");
		Course c19 = new Course("CHM", 4406, "Pharmaceutical Drug Design");
		Course c20 = new Course("PSY", 1001, "Introduction to Psychology I");
		Course c21 = new Course("PSY", 2200, "Biological Foundations of Behaviour");
		Course c22 = new Course("PSY", 3202, "Sensory Processes");
		Course c23 = new Course("PSY", 3402, "Criminal Behaviour");
		Course c24 = new Course("PSY", 3506, "Cognitive Development");
		Course c25 = new Course("PSY", 3604, "Abnormal Psychology");
		Course c26 = new Course("PSY", 4402, "Police Psychology");
		Course c27 = new Course("YUMMY", 1101, "Professional Dumpling Making");

		University cu = new University("Purdue University Calumet");

		cu.offerCourse(c1);
		cu.offerCourse(c2);
		cu.offerCourse(c3);
		cu.offerCourse(c4);
		cu.offerCourse(c5);
		cu.offerCourse(c6);
		cu.offerCourse(c7);
		cu.offerCourse(c8);
		cu.offerCourse(c9);
		cu.offerCourse(c10);
		cu.offerCourse(c11);
		cu.offerCourse(c12);
		cu.offerCourse(c13);
		cu.offerCourse(c14);
		cu.offerCourse(c15);
		cu.offerCourse(c16);
		cu.offerCourse(c17);
		cu.offerCourse(c18);
		cu.offerCourse(c19);
		cu.offerCourse(c20);
		cu.offerCourse(c21);
		cu.offerCourse(c22);
		cu.offerCourse(c23);
		cu.offerCourse(c24);
		cu.offerCourse(c25);
		cu.offerCourse(c26);

		cu.enrollStudentInCourse(s1, c1);
		cu.enrollStudentInCourse(s1, c5);
		cu.enrollStudentInCourse(s1, c11);
		cu.enrollStudentInCourse(s1, c16);
		cu.enrollStudentInCourse(s2, c4);
		cu.enrollStudentInCourse(s2, c8);
		cu.enrollStudentInCourse(s2, c15);
		cu.enrollStudentInCourse(s3, c3);
		cu.enrollStudentInCourse(s3, c7);
		cu.enrollStudentInCourse(s3, c23);
		cu.enrollStudentInCourse(s3, c24);
		cu.enrollStudentInCourse(s3, c25);
		cu.enrollStudentInCourse(s4, c8);
		cu.enrollStudentInCourse(s4, c5);
		cu.enrollStudentInCourse(s4, c11);
		cu.enrollStudentInCourse(s4, c16);
		cu.enrollStudentInCourse(s5, c7);
		cu.enrollStudentInCourse(s5, c14);
		cu.enrollStudentInCourse(s5, c18);
		cu.enrollStudentInCourse(s5, c26);
		cu.enrollStudentInCourse(s6, c8);
		cu.enrollStudentInCourse(s6, c9);
		cu.enrollStudentInCourse(s6, c10);
		cu.enrollStudentInCourse(s6, c15);
		cu.enrollStudentInCourse(s7, c2);
		cu.enrollStudentInCourse(s7, c21);
		cu.enrollStudentInCourse(s7, c17);
		cu.enrollStudentInCourse(s7, c12);
		cu.enrollStudentInCourse(s7, c13);
		cu.enrollStudentInCourse(s7, c6);
		cu.enrollStudentInCourse(s8, c3);
		cu.enrollStudentInCourse(s8, c14);
		cu.enrollStudentInCourse(s8, c22);
		cu.enrollStudentInCourse(s9, c21);
		cu.enrollStudentInCourse(s10, c1);
		cu.enrollStudentInCourse(s1, c27); // Should not work because course is not offered
		cu.enrollStudentInCourse(s4, c11); // This student is already registered in this course, and should not be added twice
		cu.enrollStudentInCourse(s4, c11); // Again this should not work


		System.out.println("The University looks as follows: " + cu); // 10 students & 26 courses
		System.out.println("Attempting to cancel \"Introductory Biology I\"...");
		cu.cancelCourse(c1); // Should not work, but should not stop program either
		System.out.println("The University still looks as follows: " + cu); // 10 students & 26 courses
		System.out.println("Attempting to cancel \"Professional Dumpling Making\"...");
		cu.cancelCourse(c27); // Should not work, but should not stop program either
		System.out.println("The University still looks as follows: " + cu); // 10 students & 26 courses
		System.out.println("Attempting to cancel \"Introduction to Psychology I\"...");
		cu.cancelCourse(c20); // Should work ok
		System.out.println("The University now looks as follows: " + cu); // 10 students & 25 courses
		System.out.println("Adding \"Introduction to Psychology I\" again... ");
		cu.offerCourse(c20);
		System.out.println("\nThe courses with no students are: " + cu.lowRegistrationCourses(0)); //only c20
		System.out.println("\nThe courses with one student or less are: " +
		cu.lowRegistrationCourses(1)); // all these c2,c4,c6,c9,c10,c12,c13,c17,c18,c19,c20,c22,c23,c24,c25,c26
		System.out.println("\nThe course with the highest enrollment is " + cu.highestEnrollment()); // Computer Graphics
		System.out.println("The busiest student is " + cu.busiestStudent());
		System.out.println("\nThe classmates of Keanu Reeves are: " + s3.classmatesAt(cu));
		System.out.println("The classmates of Clint Eastwood are: " + s7.classmatesAt(cu));
		System.out.println("The University looks as follows: " + cu); // 10 students & 26 courses
		System.out.println("Clint Eastwood is taking these courses: " + s7.getCourses()); // should be 6 courses
		System.out.println("Withdrawing Clint Eastwood from \"Microbiology\" and from \"ComputerOrganization\"...");
		cu.withdrawStudentFromCourse(s7, c2); // Should work ok
		cu.withdrawStudentFromCourse(s7, c6); // Should work ok
		System.out.println("Clint Eastwood is now taking these courses: " + s7.getCourses()); // should be 4 courses
		System.out.println("\nThe busiest student now is " + cu.busiestStudent());// Keanu Reeves
		System.out.println("\nThe courses with no students are: " + cu.lowRegistrationCourses(0)); //now c2, c6 and c20
	}
}

Recommended Answers

All 3 Replies

yes well, 'you're getting a nullpointerexception at line 35 of your university class, but unless you post that code in a separate code block, it'll be a bit difficult for us to see what that line does.

public void offerCourse(Course c){
    	courses.add(c);
    }

that is the the line

the line that calls it is

cu.offerCourse(c1);

try with :

private ArrayList<Course> courses = new ArrayList<Course>();
private ArrayList<Student> students = new ArrayList<Student>();

in your University class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.