4 Replies - 334 Views - Last Post: 07 February 2012 - 05:10 PM Rate Topic: -----

Topic Sponsor:

#1 Shaun123987  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-November 11

Null Pointer Exception

Posted 07 February 2012 - 04:14 PM

Hey everyone, I'm getting a Null Pointer Exception

Person Class:
public abstract class Person { 



	protected String name;

	protected int UFID;

	protected String dob;



	public Person(String name, int UFID, String dob) {

		this.name = name;

		this.UFID = UFID;

		this.dob = dob;

	}



	public String getName() {

		return name;

	}



	public void setName(String name) {

		this.name = name;

	}



	public int getUFID() {

		return UFID;

	}



	public void setUFID(int UFID) {

		this.UFID = UFID;

	}



	public String getDob() {

		return dob;

	}



	public void setDob(String dob) {

		this.dob = dob;

	}



	public String toString() {

		String str = "";

		str += "Name: " + name + "\n";

		str += "UFID: " + UFID + "\n";

		str += "D.O.B: " + dob + "\n";

	return str;	

	}



	public boolean equals(Object ob) { 

		if(ob instanceof Person) {

			Person p = (Person) ob;

			if (this.name.equals(p.getName()) && this.UFID == p.getUFID()){

				return true; 

			}

			else return false;

		}

		else return false;

	}

		

}	



Student Class:
public abstract class Student extends Person { 



	protected Course[] courses;

	protected final int maxCourses = 4;

	protected int numCoursesEnrolled;

	protected double gpa;



	public Student(String name, int UFID, String dob, double gpa) {

		super(name, UFID, dob);

		this.gpa = gpa;

		Course[] courses = new Course[maxCourses];

		numCoursesEnrolled = 0;

	}



	public Student(String name, int UFID, String dob, double gpa, Course[] courses) {

		super(name, UFID, dob);

		this.courses=courses;

		this.gpa = gpa;

			for(int i=0; i<courses.length; i++) {

				if(courses[i] != null){

					numCoursesEnrolled ++;

				}	

			}	

	}

	

	public int getNumCoursesEnrolled() {

		return numCoursesEnrolled;

	}



	public void setNumCoursesEnrolled(int numCoursesEnrolled) {

		this.numCoursesEnrolled = numCoursesEnrolled;

	}



	public double getGpa() {

		return gpa;

	}



	public void setGpa(double gpa) {

		this.gpa = gpa;

	}



	public abstract boolean addCourse(Course course);



	public boolean dropCourse(Course course){

		if(course.removeStudent(this)) {

			for(int i=0; i<courses.length; i++) {

				if(courses[i] == course){

					courses[i] = null;

					numCoursesEnrolled--;					

					return true;

					

				}

				else return false;

			}

		}

		else return false;
		return false;

	}



	public String toString() { 

		String str = "";

		str += super.toString();

		str += "GPA: " + gpa + "\n";

		str += "Courses enrolled in: " + "\n";

			for(int i = 0; i<courses.length; i++){
				if(courses[i] != null){

					str += "Course " + (i+1) + ":\n";

					str += courses[i].toString() + "\n";
				}
				else if(courses[i] == null){
					str += "Course " + (i+1) + ":\n";
					str += "null";
				}

				

			}
			return str;

	}		



}



Instructor Class:
public class Instructor extends Person { 

	

	private Course course;



	public Instructor(String name, int UFID, String dob){

		super(name, UFID, dob);

	}



	public Instructor(String name, int UFID, String dob, Course course){

		super(name, UFID, dob);

		this.course = course;

	}



	public void setCourse(Course course){

		this.course = course;

	}



	public Course getCourse(){

		return course;

	}



	public String getName() {

		return name;

	}



	public String toString(){

		String str = "";

		str += "Course Being Taught: " + "\n";

		str += course.toString();

		return str;
	}

}


UndergradStudent Class:
public class UndergradStudent extends Student { 

	public UndergradStudent(String name, int UFID, String dob, double gpa) {
		super(name, UFID, dob, gpa);
	}

	public UndergradStudent(String name, int UFID, String dob, double gpa, Course[] courses) {
		super(name, UFID, dob, gpa, courses);
	}

	public boolean addCourse(Course course) {
		
		for(int i=0; i<courses.length; i++) {
			if(courses[i] == null){
				if(course.getNumber() < 5000 && super.getNumCoursesEnrolled() < 4 && course.addStudent(this)){
					courses[i] = course;
					numCoursesEnrolled++;
					return true;
					
				}
				else return false;
				
			}return false;
		} return false;
	}
	
	public String toString() {
		return super.toString(); 
	}
}


public class GradStudent extends Student {
	
	private Course courseTA;

	public GradStudent(String name, int UFID, String dob, double gpa) {
		super(name, UFID, dob, gpa);
	}

	public GradStudent(String name, int UFID, String dob, double gpa, Course[] courses){
		super(name, UFID, dob, gpa, courses);
	}

	public GradStudent(String name, int UFID, String dob, double gpa, Course[] courses, Course courseTA) {
		super(name, UFID, dob, gpa, courses);
		this.courseTA = courseTA;
	}

	public Course getCourseTA() {
		return courseTA;
	}
	
	public void setCourseTA(Course courseTA) {
		this.courseTA = courseTA;
	}
	
	public boolean addCourse(Course course) {
		if(course.getNumber() >= 5000 && super.getNumCoursesEnrolled() < 4 && course.addStudent(this)){
			for(int i=0; i<courses.length; i++) {
				if(courses[i] == null){
					courses[i] = course;
					numCoursesEnrolled++;
					return true;
					
				}
			else return false;
			}
		} return false;
	}

	public String toString() {
		String str = "";
		str += super.toString() + "\n";
		str += "Course TA for: " + "\n";
		str += courseTA.toString();
		return str;
	}
}


University Class:
public class University {

	private String name;
	private String currentTerm;
	private int year;
	private Course[] courses;
	private Student[] students;
	private Instructor[] instructors;
	
	public University (String name, String currentTerm, int year, Student[] students, Instructor[] instructors, Course[] courses) {
		this.name = name;
		this.currentTerm = currentTerm;
		this.year = year;
		this.students = students;
		this.instructors = instructors;
		this.courses = courses; 
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setCurrentTerm(String currentTerm) {
		this.currentTerm = currentTerm;
	}

	public String getCurrentTerm() {
		return currentTerm;
	}

	public void setYear(int year) {
		this.year = year;
	}
		
	public int getYear() {
		return year;
	}

	public void setCourses(Course[] courses) {
		this.courses = courses; 
	}

	public Course[] getCourses() {
		return courses;
	}

	public void setStudents(Student[] students) {
		this.students = students;
	}

	public Student[] getStudents() {
		return students;
	}

	public void setInstructors(Instructor[] instructors) {
		this.instructors = instructors;
	}

	public Instructor[] getInstructors() {
		return instructors;
	}

	public String toString() {
		String str = "";
		str += "University Name: " + name + "\n";
		str += "Current Term: " + currentTerm + "\n";
		str += "Current Year: " + year + "\n";
		str += "Number of Students: " + students.length + "\n";
		str += "Number of Instructors: " + instructors.length + "\n";
		str += "Number of Courses: " + courses.length;
		return str;
	}
}


Course Class:
public class Course {

	private String type;

	private String title;

	private int number;

	private int numCredits;

	private Instructor instructor;

	private GradStudent[] TAs;

	private Student[] students;

	private int capacity;

	private int currentEnrollment;

	

	public Course(String type, int number, String title, int numCredits) {

		this.type = type;

		this.number = number;

		this.title = title;

		this.numCredits = numCredits;

	}



	public Course(String type, int number, String title, int numCredits, Instructor instructor, GradStudent[] TAs, int capacity) {

		Student[] students = new Student[capacity];

		this.type = type;

		this.number = number;

		this.title = title;

		this.numCredits = numCredits;

		this.instructor = instructor;
		this.TAs = TAs;
		this.capacity = capacity;

	

	}



	public boolean addStudent(Student student) {

		

		for(int i=0; i<students.length; i++) {

			if(students[i] != null){
				if(this.getCurrentEnrollment() < this.getCapacity()) {

					students[i] = student;

					currentEnrollment++;

					return true;

					

				}

				else return false; 

			}

		}	

		 

		return false;		
	} 



	public boolean removeStudent(Student student) {

		for(int i=0; i<students.length; i++) {

				if(this.equals(students[i])){

					students[i] = null;

					currentEnrollment--;

					return true;

				

				}

				else return false;

		}return false;

	}



	public void setType(String type) {

		this.type = type;

	}



	public String getType() {

		return type;

	}



	public void setNumber(int number) {

		this.number = number;

	}



	public int getNumber() {

		return number;

	}



	public void setTitle(String title) {

		this.title = title;

	}



	public String getTitle() {

		return title;

	}



	public void setNumCredits(int numCredits) {

		this.numCredits = numCredits;

	}



	public int getNumCredits() {

		return numCredits;

	}



	public void setCapacity(int capacity) {

		this.capacity = capacity;

		Student[] students2 = new Student[capacity];

			for(int i = 0; i<students.length; i++){

				if(students[i] != null){

					students2[i] = students[i];

				}

			}

		students = students2;

	}



	public int getCapacity() {

		return capacity;

	}



	public void setCurrentEnrollment(int currentEnrollment) {

		this.currentEnrollment = currentEnrollment;

	}



	public int getCurrentEnrollment() {

		return currentEnrollment;

	}



	public void setStudents(Student[] students) {

		this.students = students;

	}



	public Student[] getStudents() {

		return students;

	}



	public void setInstructor(Instructor instructor) {

		this.instructor = instructor;

		this.instructor.setCourse(this);

	}



	public Instructor getInstructor() {

		return instructor;

	}



	public void setTAs(GradStudent TAs[]) {

		this.TAs = TAs;

	}



	public GradStudent[] getTAs() {

		return TAs;

	}



	public String toString() {

		String str = "";

		str += "Course Info: " + "\n";

		str += type + number + "\n";

		str += "Title: " + title + "\n";

		str += "Instructor: " + instructor.getName() + "\n";

		str += "TAs:" + "\n";

			for(int i = 0; i<TAs.length; i++) {

				str += TAs[i].getName() + "\n";

			}

		str += "Number of Students: " + currentEnrollment + "\n";

		str += "Capacity: " + capacity;

		

		return str;

	}

}



Test Driver:
// Test Driver for Project 1

// COP 3503 Spring 2012



// NOTE: This driver does not test every method/condition thoroughly, but the Driver used

//       used to grade will (keep the set/get methods in mind).



import java.util.Calendar;



public class TestDriver1 {



	public static void main(String[] args) {

		

		

		// Create some Undergrad Students

		UndergradStudent student1 = new UndergradStudent("John Smith", 871311, new String("1990/01/21"), 3.5);

		UndergradStudent student2 = new UndergradStudent("Peter Parker", 871312, new String("1989/02/03"), 4.0);

		UndergradStudent student3 = new UndergradStudent("Rachel Smith", 871320, new String("1990/10/01"), 3.8);

		UndergradStudent student4 = new UndergradStudent("Karen Hicks", 871322, new String("1991/12/31"), 3.6);

		

		// Create some grad Students

		GradStudent gs1 = new GradStudent("Chao Chen", 118491, new String("1984/05/05"), 3.8);

		GradStudent gs2 = new GradStudent("Mike Smith", 128191, new String("1986/01/02"), 3.7);

		GradStudent gs3 = new GradStudent("Nathan Rodriguez", 714133, new String("1984/04/04"), 3.7);

		GradStudent gs4 = new GradStudent("Katy Chu", 144211, new String("1987/08/03"), 3.9);

		GradStudent gs5 = new GradStudent("Jay Sanders", 144215, new String("1987/11/03"), 3.5);

		

		// Create some instructors

		Instructor i1 = new Instructor("Shayan Javed", 119042, new String("1985/01/01"));

		Instructor i2 = new Instructor("Jack Davis", 119042, new String("1975/10/11"));

		

		// Create an undergrad course

		GradStudent[] tas = {gs1, gs2};

		UndergradStudent[] students1 = {student1, student3};

		System.out.println("---Creating undergrad course1 with capacity 2---");

		Course course1 = new Course("COP", 3503, "Programming Fundamentals", 3, i1, tas, 2);

		System.out.println("--" + course1.getType() + course1.getNumber() + ": " + course1.getTitle());

		// Set the course TA for the grad students

		gs1.setCourseTA(course1);

		gs2.setCourseTA(course1);

		

		// Try enrolling some students

		System.out.println("Trying to add student1 to course1: " + student1.addCourse(course1));

		System.out.println("Trying to add gs1 to course1: " + gs1.addCourse(course1));

		System.out.println("Trying to add student3 to course1: " + student3.addCourse(course1));

		System.out.println("Trying to add student2 to course1: " + student2.addCourse(course1));

		

		// Increase capacity of course1

		System.out.println("-Increasing capacity of course1 to 3--");

		course1.setCapacity(3);

		System.out.println("Trying to add student2 to course1: " + student2.addCourse(course1));

		System.out.println("student4 trying to drop course1: " + student4.dropCourse(course1));

		System.out.println("student1 trying to drop course1: " + student1.dropCourse(course1));

		System.out.println("Trying to add student4 to course1: " + student4.addCourse(course1));

		

		// Create a grad course

		GradStudent[] tas2 = {gs3};

		System.out.println("\n---Creating grad course2 with capacity 2---");

		Course course2 = new Course("CAP", 5024, "Advanced Networking", 3, i2, tas2, 2);

		System.out.println("--" + course2.getType() + course2.getNumber() + ": " + course2.getTitle());

		// Set the course TA for the grad students

		gs3.setCourseTA(course2);



		// Try enrolling some students

		System.out.println("Trying to add student1 to course2: " + student1.addCourse(course2));

		System.out.println("Trying to add gs1 to course2: " + gs1.addCourse(course2));

		System.out.println("Trying to add gs2 to course2: " + gs2.addCourse(course2));

		System.out.println("Trying to add gs4 to course2: " + gs4.addCourse(course2));

		//System.out.println("\n--Printing out course2 info:--");

		//System.out.println(course2);

		

		// Increase capacity of course2

		System.out.println("-Increasing capacity of course2 to 4--");

		course2.setCapacity(4);

		System.out.println("Trying to add gs4 to course2: " + gs4.addCourse(course2));

		System.out.println("gs5 trying to drop course2: " + gs5.dropCourse(course2));

		System.out.println("gs1 trying to drop course2: " + gs1.dropCourse(course2));

		System.out.println("Trying to add gs5 to course2: " + gs5.addCourse(course2));

		//System.out.println("\n--Printing out course2 info:--");

		//System.out.println(course2);

		

		// Create another undergrad course

		Instructor i3 = new Instructor("Larry David", 119080, new String("1965/02/15"));

		Student[] students2 = {student1, student2, student3, student4, gs3};

		GradStudent[] tas3 = {gs4, gs5};

		

		System.out.println("\n---Creating undergrad course3 with capacity 10---");

		Course course3 = new Course("SOC", 1001, "Sociology 101", 3, i3, tas3, 10);

		System.out.println("--" + course3.getType() + course3.getNumber() + ": " + course3.getTitle());

		gs4.setCourseTA(course3);

		gs5.setCourseTA(course3);

		for (Student s : students2) {

			System.out.println("Trying to add " + s.getName() + " to " + course3.getType() + course3.getNumber() + ": " + s.addCourse(course3));

		}

		

		// Print out all the students info. (Test polymorphism)

		Student[] students = {student1, student2, student3, student4, gs1, gs2, gs3, gs4, gs5};

		

		System.out.println("\n--Printing out Students info--");

		for (Student s : students)

			System.out.println("\n" + s);

		

		// Create the University object

		Course[] courses = {course1, course2, course3};

		Instructor[] instructors = {i1, i2, i3};

		University ufl = new University("University of Florida", "Spring", 2012, students, instructors, courses);

		

		System.out.println("\n--Printing out University info--");

		System.out.println(ufl);

	}



}


It gives the exception when it tries to enroll student1 into course1. Any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Null Pointer Exception

#2 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 106
  • View blog
  • Posts: 359
  • Joined: 28-November 09

Re: Null Pointer Exception

Posted 07 February 2012 - 04:53 PM

Post the stack trace

EDIT Course[] courses = new Course[maxCourses];
in Student should be
this.courses = new Course[maxCourses];

This post has been edited by ianian112: 07 February 2012 - 04:56 PM

Was This Post Helpful? 1
  • +
  • -

#3 Shaun123987  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-November 11

Re: Null Pointer Exception

Posted 07 February 2012 - 05:04 PM

---Creating undergrad course1 with capacity 2---
--COP3503: Programming Fundamentals
Exception in thread "main" java.lang.NullPointerException

---Creating undergrad course1 with capacity 2---
--COP3503: Programming Fundamentals
Exception in thread "main" java.lang.NullPointerException
at UndergradStudent.addCourse(UndergradStudent.java:13)
at TestDriver1.main(TestDriver1.java:42)

Ah okay I fixed that but now I'm getting
---Creating undergrad course1 with capacity 2---
--COP3503: Programming Fundamentals
Exception in thread "main" java.lang.NullPointerE
at Course.addStudent(Course.java:33)
at UndergradStudent.addCourse(UndergradSt
at TestDriver1.main(TestDriver1.java:42)
Was This Post Helpful? 0
  • +
  • -

#4 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 106
  • View blog
  • Posts: 359
  • Joined: 28-November 09

Re: Null Pointer Exception

Posted 07 February 2012 - 05:06 PM

same problem with your Course class
Student[] students = new Student[capacity];
should be
students = new Student[capacity];
Was This Post Helpful? 1
  • +
  • -

#5 Shaun123987  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-November 11

Re: Null Pointer Exception

Posted 07 February 2012 - 05:10 PM

Ah yes that worked. I'm not getting the output I want but at least I got rid of the error. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1