I am weirdly running into an issue where null is being printed for my contact class. It seems to not like the constructor for numbers for the PhoneNumberList. I simply want to return the count that keeps track of numbers etc, but it only returns null for getNumber and getNumberCount.
Tried changing count from private to public to just access directly as well as change name and constructors.
public class Contact{
private String name;
private PhoneNumberList numbers;
// Purpose:
// initialize this instance of Contact
// with no PhoneNumber
//
public Contact (String theName)
{
// You must allocate a PhoneNumberList here
numbers = new PhoneNumberList();
name = theName;
}
// Purpose:
// initialize this instance of Contact
// add p to the list of phone numbers associated with
// this Contact
//
public Contact (String theName, PhoneNumber p)
{
// You must allocate a PhoneNumberList here
PhoneNumberList numbers = new PhoneNumberList();
name = theName;
numbers.add(p);
}
// Purpose:
// return the name associated with this instance
//
public String getName ()
{
return name;
}
// Purpose:
// change the name associated with this instance to be newName
//
public void setName(String newName)
{
name = newName;
}
// Purpose:
// add a new PhoneNumber to this contact
// there is no maximum number of phone numbers that can be
// assigned to a contact.
//
public void addNumber (PhoneNumber p)
{
numbers.add(p);
}
// Purpose:
// remove p from the list of PhoneNumbers associated with this contact
// if p is not in the list, do nothing.
//
public void removeNumber (PhoneNumber p)
{
int index = numbers.find(p);
numbers.remove(index);
}
// Purpose:
// return the count of PhoneNumbers associated with this contact
//
public int getNumberCount()
{
return numbers.count;
}
// Purpose:
// return the PhoneNumber at index pos from this contact
//
// Pre-condition:
// pos >= 0 AND
// pos < this.getNumberCount()
//
public PhoneNumber getNumber (int pos)
{
// NOTE NOTE NOTE
//
// This line needs to be removed. It is only
// so the tester works. You should NOT
// allocate a new PhoneNumber in this method
return numbers.get(pos);
}
// Purpose:
// return a String representation of this contact
//
public String toString()
{
String s = name;
for (int i=0;i<numbers.size();i++)
{
s += "\n";
s += numbers.get(i);
}
return s;
}
}
public class PhoneNumberList
{
private static final int INITIAL_SIZE = 2;
private PhoneNumber[] storage;
public int count;
//
// Purpose:
// Initialize a new instance of PhoneNumberList
//
public PhoneNumberList()
{
count = 0;
storage = new PhoneNumber[INITIAL_SIZE];
}
//
// Purpose:
// return the element at position index
//
// Pre-Conditions:
// for a PhoneNumberList x:
// index >= 0 AND
// index < x.size()
//
// Examples:
//
// If x is {"Work:5551212", "Home:4441212", "Cell:3331212"} then:
// x.get(0) returns "Work:5551212"
// x.get(1) returns "Home:4441212"
// the result of calling x.get(3) is undefined
//
public PhoneNumber get (int index)
{
// NOTE NOTE NOTE
//
// This line needs to be removed. It is only
// so the tester works. You should NOT
// allocate a new PhoneNumber in this method
return storage[index];
}
//
// Purpose:
// remove the element at position index
//
// Pre-Conditions:
// for a PhoneNumberList x:
// index >= 0 AND
// index < x.size()
//
// If x is {"Work:5551212", "Home:4441212", "Cell:3331212"} then
// after x.remove(0), x is {"Home:4441212", "Cell:3331212"}
//
public void remove (int index)
{
storage[index] = storage[count - 1];
count--;
}
//
// Purpose:
// return the number of elements in the list
//
// Returns:
// the number of elements in the list
//
// Examples:
//
// If x is {"Work:5551212", "Home:4441212"}
// x.size() returns 2
// If x is {}
// x.size() returns 0
//
public int size()
{
return this.count;
}
//
// Purpose:
// add the phone number p to the list
//
// Comments:
//
// The array you allocated to store PhoneNumbers might
// get full, but you are still required to add this
// PhoneNumber (until the JVM runs out of memory!)
//
// This means that you should check to see if the array
// is currently full. If it is, allocate a new array
// that is twice as big, then copy the values over
// and update the storage reference to be the new array
// Finally, add the new PhoneNumber.
//
public void add (PhoneNumber p)
{
if(count == storage.length){
PhoneNumber[] newStorage = new PhoneNumber[count * 2];
for (int i = 0; i < storage.length; i++){
newStorage[i] = storage[i];
}
storage = newStorage;
}
storage[count] = p;
count = count + 1;
}
//
// Purpose:
// return the index where p is in the list, -1 otherwise
//
// Pre-Conditions:
// none
//
// Returns:
// position of p in the list - an integer between 0 and size() - 1
// -1 if p is not in the list
//
// Examples:
//
// If x is {"Work:5551212", "Home:4441212", "Cell:3331212"} then
//
// PhoneNumber p = new PhoneNumber("5551212");
// PhoneNumber q = new PhoneNumber("3331212");
// PhoneNumber r = new PhoneNumber("1234567");
//
// x.find(p) returns 0
// x.find(q) returns 2
// x.find(r) returns -1
//
public int find (PhoneNumber p)
{
for(int x = 0; x < count; x++){
if (storage[x].equals(p)){
return x;
}
}
return -1;
}
}
java.lang.NullPointerException
at Contact.getNumberCount(Contact.java:72)
at a2tester.ContactTest(a2tester.java:205)
at a2tester.main(a2tester.java:224)