473,505 Members | 14,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Arraylike NullPointerException

2 New Member
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 NullPointerException 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.Scanner;
import java.util.ArrayList;

/**
* 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.getClass())
{
return false;
}
else
{
Contact otherContact = (Contact)otherObject;
return (first.equals(otherContact.first) &&
last.equals(otherContact.last)&&
phone.equals(otherContact.phone)&&
email.equals(otherContact.email));
}
}

}


/**
* 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<Contact> 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 createContactDatabase()
{
contacts = new ArrayList<Contact>();

}

/**
* 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.println("Enter the first name of a the person you wish to add, ie Karen");
String first = input.nextLine();

System.out.println("Enter the last name of a the person you wish to add, ie Brown");
String last = input.nextLine();

System.out.println("Enter your contact's phone number.");
String phone = input.nextLine();

System.out.println("Enter your contact's email address.");
String email = input.nextLine();

myContact = new Contact(first, last, phone, email);
contacts.add(myContact);


System.out.println("The contacts details are:");
System.out.println(contacts);
}


/**
* displayAll iterates through the ArrayList of contacts and outputs each one
* to the screen.
*/
public void displayAll()
{
System.out.println("The contacts list contains:");
for (Contact entry : contacts)
System.out.println (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.indexOf("first");
System.out.println("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.println();
System.out.println("Choose from the following:");
System.out.println("0) Quit");
System.out.println("1) Add new contact");
System.out.println("2) List all contacts");
System.out.println("3) Search contacts by keyword and display");
System.out.println("4) Search contacts by keyword and remove");
choice = scan.nextInt();
switch (choice)
{
case ADD: cdb.inputContact();
break;
case LISTALL: cdb.displayAll();
break;
case SEARCH: cdb.displayMatch();
break;
case DELETE: cdb.deleteMatch();
break;
}
}
}
} // ContactDatabase
Mar 20 '07 #1
5 1439
RedSon
5,000 Recognized Expert Expert
Why do you have this method: createContactDatabase()?
Mar 20 '07 #2
Ganon11
3,652 Recognized Expert Specialist
So that's it!

You have the method createContactDatabase(), but from your documentation, this should be a constructor! Rename it as public ContactDatabase(), and when you create the object in main(), it will create its own Arraylist and get rid of the NullPointerException.
Mar 20 '07 #3
r035198x
13,262 MVP
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 NullPointerException 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.Scanner;
import java.util.ArrayList;

/**
* 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.getClass())
{
return false;
}
else
{
Contact otherContact = (Contact)otherObject;
return (first.equals(otherContact.first) &&
last.equals(otherContact.last)&&
phone.equals(otherContact.phone)&&
email.equals(otherContact.email));
}
}

}


/**
* 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<Contact> 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 createContactDatabase()
{
contacts = new ArrayList<Contact>();

}

/**
* 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.println("Enter the first name of a the person you wish to add, ie Karen");
String first = input.nextLine();

System.out.println("Enter the last name of a the person you wish to add, ie Brown");
String last = input.nextLine();

System.out.println("Enter your contact's phone number.");
String phone = input.nextLine();

System.out.println("Enter your contact's email address.");
String email = input.nextLine();

myContact = new Contact(first, last, phone, email);
contacts.add(myContact);


System.out.println("The contacts details are:");
System.out.println(contacts);
}


/**
* displayAll iterates through the ArrayList of contacts and outputs each one
* to the screen.
*/
public void displayAll()
{
System.out.println("The contacts list contains:");
for (Contact entry : contacts)
System.out.println (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.indexOf("first");
System.out.println("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.println();
System.out.println("Choose from the following:");
System.out.println("0) Quit");
System.out.println("1) Add new contact");
System.out.println("2) List all contacts");
System.out.println("3) Search contacts by keyword and display");
System.out.println("4) Search contacts by keyword and remove");
choice = scan.nextInt();
switch (choice)
{
case ADD: cdb.inputContact();
break;
case LISTALL: cdb.displayAll();
break;
case SEARCH: cdb.displayMatch();
break;
case DELETE: cdb.deleteMatch();
break;
}
}
}
} // ContactDatabase
Please use code tags when posting code.
Also find time to read this.
Mar 21 '07 #4
annfarrell32
2 New Member
Please use code tags when posting code.
Also find time to read this.
Thanks, I never stopped to think about correcting the code that was provided. Just assumed it would be correct. You learn something new everyday.
Thanks again,
Annie
Mar 22 '07 #5
RedSon
5,000 Recognized Expert Expert
The code above is correct it just follows poor programming practices.
Mar 23 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
8560
by: K S Aldebaraan | last post by:
I'm trying to submit a form with an action of a servlet, and a view equal to the same jsp page. I'm not sure what I'm doing wrong, but keep getting a NullPointerException on the second line of...
4
4057
by: gabryh | last post by:
Hi, The following code throws me NullPointerException. ..... public static boolean isEmpty(String value) { return ((value == null) || (value.trim().equals(""))); }
0
4404
by: Old-timer | last post by:
Not sure where else to post this. I'm sure I'm doing something wrong, but I wouldn't think a simple app would give me so much trouble. I've got a small test java class that I'm trying to have...
13
1947
oll3i
by: oll3i | last post by:
private List<Klient> klienci; m_add_client.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { ...
1
2401
by: ketand1 | last post by:
import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import java.sql.*; import java.lang.*; class DbAwt extends Frame implements ActionListener { private...
2
2261
by: sokeefe | last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that...
1
3182
by: sokeefe | last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that...
1
17521
by: r035198x | last post by:
This exception occurs often enough in practice to warrant its own article. It is a very silly exception to get because it's one of the easiest exceptions to avoid in programming. Yet we've all got it...
3
3321
by: chris123456789 | last post by:
Hi, when I run my code I get a NullPointerException:null. Here is the part of the code where the error occurs: import java.util.*; import java.io.*; public class Decrypt { ...
0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5028
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.