Hi,
I am currently taking Java II because I want to. Unfortunately, the teacher isn't the best and neither is the book. I'm getting a NullPointerExce ption when adding a contact to the arraylist ContactDatabase . I would just like to know what I'm overlooking so I can correct and continue with this lesson. I know the Contact is not being instantiated, but am at a lost for what I'm doing wrong. I appreciate any and all help. Thanks Annie.
Here's the code.
/**
* ContactDatabase .java
*
* This program implements a database of contacts that can be
* added to, searched, displayed, or items removed. An ArrayList
* is used to store the database.
*
* Created: Sun Apr 3 2005
* Updated: Sun July 30 2006 Amy Hickey
*
* @author Kenrick Mock
* @version 1
*
*/
import java.util.Scann er;
import java.util.Array List;
/**
* The Contact class stores the details for a single contact. There is no error
* checking on any of the input. Whatever string is passed in for a given
* attribute is accepted.
*/
class Contact
{
private String first, last, phone, email;
/**
* Constructors.
*/
public Contact()
{
}
public Contact(String first, String last, String phone, String email)
{
this.first = first;
this.last = last;
this.phone = phone;
this.email = email;
}
/*
* Accessor Methods
*/
public String getFirst()
{
return first;
}
public String getLast()
{
return last;
}
public String getPhone()
{
return phone;
}
public String getEmail()
{
return email;
}
/*
* Mutator Methods
*/
public void setFirst(String first)
{
this.first = first;
}
public void setLast(String last)
{
this.last = last;
}
public void setPhone(String phone)
{
this.phone = phone;
}
public void setEmail(String em)
{
this.email = em;
}
/*
* Return all fields concatenated into a string
*/
public String toString()
{
return last + ", " + first + ". " + phone + ", " + email;
}
public boolean equals(Object otherObject)
{
if (otherObject ==null)
{
return false;
}
else if (getClass() != otherObject.get Class())
{
return false;
}
else
{
Contact otherContact = (Contact)otherO bject;
return (first.equals(o therContact.fir st) &&
last.equals(oth erContact.last) &&
phone.equals(ot herContact.phon e)&&
email.equals(ot herContact.emai l));
}
}
}
/**
* The ContactDatabase class stores each contact in an arraylist.
* Methods exist to add new contacts, search contacts, delete, and print contacts
* to the console.
*/
public class ContactDatabase
{
private ArrayList<Conta ct> contacts; // ArrayList of contact
private static final int QUIT = 0; // Menu choices
private static final int ADD = 1;
private static final int LISTALL = 2;
private static final int SEARCH = 3;
private static final int DELETE = 4;
/**
* Default constructor - make a new ArrayList object with parameter type Contact
*/
public void createContactDa tabase()
{
contacts = new ArrayList<Conta ct>();
}
/**
* inputContact inputs contact information from the keyboard.
* It then stores this new contact in the contacts ArrayList.
*/
public void inputContact()
{
Contact myContact;
Scanner input = new Scanner(System. in);
System.out.prin tln("Enter the first name of a the person you wish to add, ie Karen");
String first = input.nextLine( );
System.out.prin tln("Enter the last name of a the person you wish to add, ie Brown");
String last = input.nextLine( );
System.out.prin tln("Enter your contact's phone number.");
String phone = input.nextLine( );
System.out.prin tln("Enter your contact's email address.");
String email = input.nextLine( );
myContact = new Contact(first, last, phone, email);
contacts.add(my Contact);
System.out.prin tln("The contacts details are:");
System.out.prin tln(contacts);
}
/**
* displayAll iterates through the ArrayList of contacts and outputs each one
* to the screen.
*/
public void displayAll()
{
System.out.prin tln("The contacts list contains:");
for (Contact entry : contacts)
System.out.prin tln (entry);
}
/**
* displayMatch inputs a keyword from the user.
* It then iterates through the ArrayList of contacts and outputs each one
* to the screen if the contact information contains the keyword.
*/
public void displayMatch()
{
int locationIndex = contacts.indexO f("first");
System.out.prin tln("Index location of the String \"Jeff\" is: " + locationIndex);
}
/**
* deleteMatch inputs a keyword from the user.
* It then iterates through the ArrayList of contacts and asks the user
* if the contact should be deleted, if the contact information contains the keyword.
*/
public void deleteMatch()
{
}
// Main class
public static void main(String[] args)
{
ContactDatabase cdb = new ContactDatabase ();
Scanner scan = new Scanner(System. in);
int choice = ADD;
// Main menu
while (choice != QUIT)
{
System.out.prin tln();
System.out.prin tln("Choose from the following:");
System.out.prin tln("0) Quit");
System.out.prin tln("1) Add new contact");
System.out.prin tln("2) List all contacts");
System.out.prin tln("3) Search contacts by keyword and display");
System.out.prin tln("4) Search contacts by keyword and remove");
choice = scan.nextInt();
switch (choice)
{
case ADD: cdb.inputContac t();
break;
case LISTALL: cdb.displayAll( );
break;
case SEARCH: cdb.displayMatc h();
break;
case DELETE: cdb.deleteMatch ();
break;
}
}
}
} // ContactDatabase
I am currently taking Java II because I want to. Unfortunately, the teacher isn't the best and neither is the book. I'm getting a NullPointerExce ption when adding a contact to the arraylist ContactDatabase . I would just like to know what I'm overlooking so I can correct and continue with this lesson. I know the Contact is not being instantiated, but am at a lost for what I'm doing wrong. I appreciate any and all help. Thanks Annie.
Here's the code.
/**
* ContactDatabase .java
*
* This program implements a database of contacts that can be
* added to, searched, displayed, or items removed. An ArrayList
* is used to store the database.
*
* Created: Sun Apr 3 2005
* Updated: Sun July 30 2006 Amy Hickey
*
* @author Kenrick Mock
* @version 1
*
*/
import java.util.Scann er;
import java.util.Array List;
/**
* The Contact class stores the details for a single contact. There is no error
* checking on any of the input. Whatever string is passed in for a given
* attribute is accepted.
*/
class Contact
{
private String first, last, phone, email;
/**
* Constructors.
*/
public Contact()
{
}
public Contact(String first, String last, String phone, String email)
{
this.first = first;
this.last = last;
this.phone = phone;
this.email = email;
}
/*
* Accessor Methods
*/
public String getFirst()
{
return first;
}
public String getLast()
{
return last;
}
public String getPhone()
{
return phone;
}
public String getEmail()
{
return email;
}
/*
* Mutator Methods
*/
public void setFirst(String first)
{
this.first = first;
}
public void setLast(String last)
{
this.last = last;
}
public void setPhone(String phone)
{
this.phone = phone;
}
public void setEmail(String em)
{
this.email = em;
}
/*
* Return all fields concatenated into a string
*/
public String toString()
{
return last + ", " + first + ". " + phone + ", " + email;
}
public boolean equals(Object otherObject)
{
if (otherObject ==null)
{
return false;
}
else if (getClass() != otherObject.get Class())
{
return false;
}
else
{
Contact otherContact = (Contact)otherO bject;
return (first.equals(o therContact.fir st) &&
last.equals(oth erContact.last) &&
phone.equals(ot herContact.phon e)&&
email.equals(ot herContact.emai l));
}
}
}
/**
* The ContactDatabase class stores each contact in an arraylist.
* Methods exist to add new contacts, search contacts, delete, and print contacts
* to the console.
*/
public class ContactDatabase
{
private ArrayList<Conta ct> contacts; // ArrayList of contact
private static final int QUIT = 0; // Menu choices
private static final int ADD = 1;
private static final int LISTALL = 2;
private static final int SEARCH = 3;
private static final int DELETE = 4;
/**
* Default constructor - make a new ArrayList object with parameter type Contact
*/
public void createContactDa tabase()
{
contacts = new ArrayList<Conta ct>();
}
/**
* inputContact inputs contact information from the keyboard.
* It then stores this new contact in the contacts ArrayList.
*/
public void inputContact()
{
Contact myContact;
Scanner input = new Scanner(System. in);
System.out.prin tln("Enter the first name of a the person you wish to add, ie Karen");
String first = input.nextLine( );
System.out.prin tln("Enter the last name of a the person you wish to add, ie Brown");
String last = input.nextLine( );
System.out.prin tln("Enter your contact's phone number.");
String phone = input.nextLine( );
System.out.prin tln("Enter your contact's email address.");
String email = input.nextLine( );
myContact = new Contact(first, last, phone, email);
contacts.add(my Contact);
System.out.prin tln("The contacts details are:");
System.out.prin tln(contacts);
}
/**
* displayAll iterates through the ArrayList of contacts and outputs each one
* to the screen.
*/
public void displayAll()
{
System.out.prin tln("The contacts list contains:");
for (Contact entry : contacts)
System.out.prin tln (entry);
}
/**
* displayMatch inputs a keyword from the user.
* It then iterates through the ArrayList of contacts and outputs each one
* to the screen if the contact information contains the keyword.
*/
public void displayMatch()
{
int locationIndex = contacts.indexO f("first");
System.out.prin tln("Index location of the String \"Jeff\" is: " + locationIndex);
}
/**
* deleteMatch inputs a keyword from the user.
* It then iterates through the ArrayList of contacts and asks the user
* if the contact should be deleted, if the contact information contains the keyword.
*/
public void deleteMatch()
{
}
// Main class
public static void main(String[] args)
{
ContactDatabase cdb = new ContactDatabase ();
Scanner scan = new Scanner(System. in);
int choice = ADD;
// Main menu
while (choice != QUIT)
{
System.out.prin tln();
System.out.prin tln("Choose from the following:");
System.out.prin tln("0) Quit");
System.out.prin tln("1) Add new contact");
System.out.prin tln("2) List all contacts");
System.out.prin tln("3) Search contacts by keyword and display");
System.out.prin tln("4) Search contacts by keyword and remove");
choice = scan.nextInt();
switch (choice)
{
case ADD: cdb.inputContac t();
break;
case LISTALL: cdb.displayAll( );
break;
case SEARCH: cdb.displayMatc h();
break;
case DELETE: cdb.deleteMatch ();
break;
}
}
}
} // ContactDatabase
Comment