I keep getting a null pointer exception at run time.
public class MobilePhone {
ArrayList<Contacts> contactsArray;
private String myNumber;
public MobilePhone(ArrayList<Contacts> contactsArray, String myNumber) {
super();
this.contactsArray = new ArrayList<Contacts>();
this.myNumber = myNumber;
}
public MobilePhone(){
}
public void addContact(Contacts contact) {
//contactsArray.contains(name);
contactsArray.add(contact);
}
I have the addContact method take a Contacts object to add to the arraylist and i call that method in the createContact method in the demo class below, where i also create the object and set the arguments. Ive tried it a couple of different ways, but the object ends up never being created.
public class challengeDemo {
static MobilePhone phone = new MobilePhone();
static Scanner input = new Scanner(System.in);
public static void main(String[]args){
createContact();
//System.out.println(phone.contactsArray.get(0));
}
public static void createContact(){
System.out.println("Enter Name: ");
String userName = input.next();
System.out.println("Enter Number: ");
String userNumber = input.next();
phone.addContact(new Contacts(userName,userNumber));
}
}
}
I also have a contacts constructor.
public class Contacts {
private String name;
private String phoneNumber;
public Contacts(String name,String phoneNumber){
this.name = name;
this.phoneNumber = phoneNumber;
}