3

good day.. i have an Address Book Program... it runs properly but it doesn't check if the user input was already stored in an array.. want i want to do is... after getting the user input compare it to the stored entry in an array and when it was unique... it will allow the user to add a new entry...

here's my old code in addEntry():

public void addEntry() {

entry[counter] = new AddressBookEntry();
entry[counter].setName(JOptionPane.showInputDialog("Enter name: "));
entry[counter].setAdd(JOptionPane.showInputDialog("Enter add: "));
entry[counter].setPhoneNo(JOptionPane.showInputDialog("Enter Phone No.: "));
entry[counter].setEmail(JOptionPane.showInputDialog("Enter E-mail: "));
counter++;

}

and here's what i am planning to do but it turns out to be an ERROR:

public void addEntry() {

    entry[counter] = new AddressBookEntry();
    SName = JOptionPane.showInputDialog("Enter name: ");//<-- asks user for the name
    if (!entry[counter].getName().equals(SName)) {//<--compare
        entry[counter].setName(JOptionPane.showInputDialog("Enter name: "));
        entry[counter].setAdd(JOptionPane.showInputDialog("Enter add: "));
        entry[counter].setPhoneNo(JOptionPane.showInputDialog("Enter Phone No.: "));
        entry[counter].setEmail(JOptionPane.showInputDialog("Enter E-mail: "));
        counter++;
    }
}

This is the error: Exception in thread "main" java.lang.NullPointerException at AddressBook.addEntry(AddressBook.java:57) at AddressBook.main(AddressBook.java:28) Java Result: 1 BUILD SUCCESSFUL (total time: 4 seconds)

the output only ask user to input for a name and then exit automatically... this might be a logical error... but i don't know what is the possible solution

Need Help please thanks

here's my complete code:

import javax.swing.JOptionPane;
import javax.swing.JTextArea;

public class AddressBook {

    private AddressBookEntry entry[];
    private int counter;
    private String SName;
    private int notfound = 0;

    public static void main(String[] args) {
        AddressBook a = new AddressBook();
        a.entry = new AddressBookEntry[100];
        int option = 0;
        try {
            while (option != 5) {
                String content = "Choose an Option\n\n"
                        + "[1] Add an Entry\n"
                        + "[2] Delete an Entry\n"
                        + "[3] Update an Entry\n"
                        + "[4] View all Entries\n"
                        + "[5] View Specific Entry\n"
                        + "[6] Exit";
                option = Integer.parseInt(JOptionPane.showInputDialog(content));
                switch (option) {
                    case 1:
                        a.addEntry();
                        break;
                    case 2:
                        a.deleteEntry();
                        break;
                    case 3:
                        a.editEntry();
                        break;
                    case 4:
                        a.viewAll();
                        break;
                    case 5:
                        a.searchEntry();
                        break;
                    case 6:
                        System.exit(1);
                        break;
                    default:
                        JOptionPane.showMessageDialog(null, "Invalid Choice!");
                }
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Please Choose a Number in the displayed Menu");
        }
    }

    public void addEntry() {
        entry[counter] = new AddressBookEntry();
        SName = JOptionPane.showInputDialog("Enter name: ");
        if (entry[counter] == null &&  entry[counter].getName() == null
                && !entry[counter].getName().equals(SName)) {
            entry[counter].setName(JOptionPane.showInputDialog("Enter name: "));
            entry[counter].setAdd(JOptionPane.showInputDialog("Enter add: "));
            entry[counter].setPhoneNo(JOptionPane.showInputDialog("Enter Phone No.: "));
            entry[counter].setEmail(JOptionPane.showInputDialog("Enter E-mail: "));
            counter++;
        }
    }
    /*public void addEntry() {
    entry[counter] = new AddressBookEntry();
    entry[counter].setName(JOptionPane.showInputDialog("Enter name: "));
    entry[counter].setAdd(JOptionPane.showInputDialog("Enter add: "));
    entry[counter].setPhoneNo(JOptionPane.showInputDialog("Enter Phone No.: "));
    entry[counter].setEmail(JOptionPane.showInputDialog("Enter E-mail: "));
    counter++;
    }*/

    public void viewAll() {
        String addText = "  NAME\tADDRESS\tPHONE NO.\tE-MAIL ADD\n\n";
        int nonNull = 0;
        for (int i = 0; i < entry.length; i++) {
            if (entry[i] != null) {
                addText = addText + entry[i].getInfo() + "\n";
                nonNull++;
            }
            if (nonNull == counter) {
                break;
            }
        }
        JOptionPane.showMessageDialog(null, new JTextArea(addText));
    }

    public void searchEntry() {
        SName = JOptionPane.showInputDialog("Enter Name to find: ");
        searchMethod();
    }

    public void searchMethod() {
        for (int i = 0; i < counter; i++) {
            if (entry[i].getName().equals(SName)) {
                JOptionPane.showMessageDialog(null, entry[i].getInfo2());
                notfound = 0;
                break;
            } else {
                notfound++;
            }
        }
        if (notfound != 0) {
            JOptionPane.showMessageDialog(null, "Name Not Found!");
        }
    }

    public void editEntry() {
        SName = JOptionPane.showInputDialog("Enter Name to edit: ");
        for (int i = 0; i < counter; i++) {
            if (entry[i].getName().equals(SName)) {
                entry[i] = new AddressBookEntry();
                entry[i].setName(JOptionPane.showInputDialog("Enter new name: "));
                entry[i].setAdd(JOptionPane.showInputDialog("Enter new add: "));
                entry[i].setPhoneNo(JOptionPane.showInputDialog("Enter new Phone No.: "));
                entry[i].setEmail(JOptionPane.showInputDialog("Enter new E-mail: "));
                notfound = 0;
                break;
            } else {
                notfound++;
            }
        }
        if (notfound != 0) {
            JOptionPane.showMessageDialog(null, "Name Not Found!");
        }
    }

    public void deleteEntry() {
        SName = JOptionPane.showInputDialog("Enter Name to delete: ");
        if (SName == null) {
            return;
        }
        for (int i = 0; i < counter; i++) {
            if (entry[i] != null && SName.equals(entry[i].getName())) {
                entry[i] = null;
                JOptionPane.showMessageDialog(null, "Found!");
                break;
            }
        }
    }
}

hope you can help me... because i still dont understand what to do. So i just show my complete code... thanks for your patience guys...

1
  • You can use a Set to automatically remove duplicates. See java.util.Set. Commented Feb 19, 2011 at 5:27

6 Answers 6

2

Check to make sure your entry isn't null before you try doing getName() from it.

if (!entry[counter].getName().equals(SName))

turns into:

if  (entry[counter] != null 
 &&  entry[counter].getName() != null 
 && !entry[counter].getName().equals(SName))
Sign up to request clarification or add additional context in comments.

7 Comments

@glowcoder.. i try to replace it to my old code... but still same output... it only asks to enter name and automatically exists the program... :(
@iamanapprentice Unless the constructor for AddressBookEntry is setting a state value for getName() to return, then the if statement will always evaluate to false, and the behavior you'll see is an immediate exit after the dialog (since there is no code after the if block).
Why are you comparing in the first place? As in, why did you go from your first version to your second version?
Also, when you use that code I put there... does it throw exception as well as exit?
@glowcoder i also include in my edited post my implementation of your given code... is that right?
|
0

Refactor to use a java.util.Map where the name is the key and the AddressBookEntry is the value. Then simply check whether the Map.containsKey(SName). And before that check on null and zero length.

Note that regardless, in your current setup, you'd want to check ALL the entries in your array, not just the current. Thats indexes i = 0 ... i < counter.

Comments

0

When you're calling AddressBook.addEntry, make sure you check if what you're passing isn't null. Either that or you're trying to add something to an array at a position that doesn't exist.

Comments

0
entry[counter] = new AddressBookEntry();

You create a new address book entry and then access the name via getName which will be null, because your entry is empty (in opposite to the other entries in your array which you filled with setName, etc.).

Plus: You said, you want to make sure every entry is unique. Why do you then insert a new entry into your array without checking before? Loop through you whole array and compare every entry's name with the user input. Only if you don't find any matches, proceed and add a new entry.

Comments

0

It seems that the constructor for AddressBookEntry doesn't instantiate a value for getName(). Unless it does somehow, then entry[counter].getName() will always be null. If that object is always null, then calling .equals() will result in the NullPointerException that you see.

Suggestions:

  • What are you checking for with the .equals()? If the constructor for AddressBookEntry doesn't set a value, then this line can never be true. If it does set a value, try using an empty string instead of a null.
  • Perform a null check first per glowcoder's answer.

Comments

0

The usual practice is to create a reference to an object and then assign it to the position in the array, so instead of doing this:

entry[counter] = new AddressBookEntry();
entry[counter].setXxx();
...

It is preferred to do:

AddressBookEntry newEntry = new AddressBookEntry();
newEntry.setXxx();
...
entry[counter] = newEntry;

This way you can then iterate over the array and detect if the entry has already been already inserted and not insert it again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.