#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    New Programming Student, Having Trouble With Abstract Classes/Interfaces


    As the title states, I am a new programming student having issues with the concept of abstract classes and interfaces. My current instructor is definitely not qualified to teach Java so I find myself in a bit of a pinch.

    Our class assignment involves creating an interface, abstract class, and concrete class to build a registry (array of sorts) to store students' names (string) and majors (string). Sounds simple on its face, but the assignment was to use three classes that were already provided for us, and "fill in the blanks" with the remaining necessary classes. The classes given to us are a Generic Registry abstract class, which is supposed to implement a Registry interface (that we are supposed to create), a test class for compiling the program, and a Student class, which creates a Comparable Student object to be used in the Student Registry class (extends the abstract Generic Registry class), which is to include a print() method. I apologize for the wall of text, hopefully the code below will clarify what I'm struggling with:

    Java Code:
     
    public abstract class GenericRegistry implements Registry {
     
    	protected Comparable[] members;
    	protected int count;
     
     
    	public void addMember(Comparable object) {
    		members[count++] = object;
    	}
     
    	// Selection sort
    	public void sort() {
     
    		for (int i = 0; i < count - 1; i++) {
     
    			int min = i;
    			for (int j = i + 1; j < count; j++) {
     
    				if (members[min].compareTo(members[j]) > 0) {
    					min = j;
    				}
    			}
     
    			Comparable object = members[i];
    			members[i] = members[min];
    			members[min] = object;
    		}
    	}
    }


    Note: the storage in this class is an array of Comparable objects. Thus, any objects to be stored in the array should be a type of Comparable.

    Java Code:
     
    public class Student implements Comparable {
     
    	private String name;
    	private String major;
     
     
    	public Student(String name, String major) {
    		this.name = name;
    		this.major = major;
    	}
     
     
    	public String getName() {
    		return name;
    	}
     
     
    	public String getMajor() {
    		return major;
    	}
     
     
    	public int compareTo(Object student) {
    		return name.compareTo(((Student) student).name);
    	}
     
     
    	public String toString() {
    		return "Name: " + name + " major " + major;
    	}
    }


    Note: this class implements the Comparable interface. Thus, it can be stored in the array called “members” in the GenericRegistry. An interface is also a type. When a class implements an interface, the class will have two types. In this case, the class Student is both a type of Student and a type of Comparable.

    Java Code:
     
    public class Lab3 {
     
    	public static void main(String[] s) {
     
    		StudentRegistry sr = new StudentRegistry(10);
    		sr.addMember(new Student("Close", "English"));
    		sr.addMember(new Student("Snow", "Physics"));
    		sr.addMember(new Student("Lal", "Physics"));
    		sr.addMember(new Student("Chow", "English"));
    		sr.print();
    		sr.sort();
    		sr.print();
    	}
    }


    Note: we allocate 10 cells for the array members. This is the largest number of Student objects that can be stored in the array. When you write the print() method for the class StudentRegistry, you should use the instance variable count in the class GenericRegistry to decide the actual number of items in the array members.

    ...Now, here is my code for the assigned classes, which is eliciting an "Exception in thread "main" java.lang.NullPointerException" error:

    Java Code:
     
    public interface Registry {
     
    	public void addMember(Comparable object);
    	public void sort();
    }

    ^ This is my interface class to be implemented by the abstract class above.

    Java Code:
     
    public class StudentRegistry extends GenericRegistry{
     
    	private int size;
     
    	public StudentRegistry(int size){
    		this.size = size;	
    	}
     
    	public void print(){
    		System.out.println("The registry contains" + count + "members.");
    	}
    }

    ^ This is my concrete StudentRegistry class.

    Thanks a MILLION to anyone who can even remotely get headed in the right direction!

    -Tony
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1958
    If you have an error message, please post the full message. It usually have a hint to where in the code the error is.

    You will have to use the keyword 'super' to access variable from the base class.

    Instead of
    Code:
    System.out.println("The registry contains" + count + "members.");
    Try with this:
    Code:
    System.out.println("The registry contains" + super.count + "members.");
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0
    Thank you for the response. Unfortunately, I am still getting the same error message, which reads:

    Exception in thread "main" java.lang.NullPointerException
    at lab3.GenericRegistry.addMember(GenericRegistry.java:10)
    at lab3.TestLab3.main(TestLab3.java:8)

    Whereby I think they are referring to this statement:

    members[count++] = object;
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1958
    You will have to tell how many members you want using the size value.

    Try add this in the StudentRegistry constructor:
    Code:
    super.members = new  Comparable[size];
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Cross posted at http://www.javaprogrammingforums.com/object-oriented-programming/13905-new-programming-student-having-trouble-abstract-classes-interfaces.html
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    Originally Posted by bulx0001
    Thank you for the response. Unfortunately, I am still getting the same error message, which reads:

    Exception in thread "main" java.lang.NullPointerException
    at lab3.GenericRegistry.addMember(GenericRegistry.java:10)
    at lab3.TestLab3.main(TestLab3.java:8)

    Whereby I think they are referring to this statement:

    members[count++] = object;
    That's definitely where the issue is. I believe the problem is that you are not initializing your members array. I recommend writing a constructor for your GenericRegistry class. The constructor should initialize your array. Then add a call to your superclass in the constructor of your StudentRegistry. You don't need to store the size for both registries because the classes are in a hierarchy. You would be duplicating information by doing that. That should fix the null pointer problem.

IMN logo majestic logo threadwatch logo seochat tools logo