Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3990706
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:31:38+00:00 2026-05-20T06:31:38+00:00

I cant figure out how to start a method to delete a specific entry

  • 0

I cant figure out how to start a method to delete a specific entry stored in an array…

I used to do this:

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

but I was advised not to assign the entry[i] to null because it will ruin my entries…

I have no idea how to code it in another way…

What should I need to do is:
I need to delete a specific entry from an array
please help…

also… its output was error it says:

Exception in thread “main” java.lang.NullPointerException
at AddressBook.viewAll(AddressBook.java:62)
at AddressBook.main(AddressBook.java:36)
Java Result: 1

This is my code in my main program:

public class AddressBook {

    private AddressBookEntry entry[];
    private int counter;
    private String SName;

    public static void main(String[] args) {
        AddressBook a = new AddressBook();
        a.entry = new AddressBookEntry[100];
        int option = 0;
        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!");
            }
        }
    }

    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";
        for (int i = 0; i < counter; i++) {
            addText = addText + entry[i].getInfo() + "\n";
        }
        JOptionPane.showMessageDialog(null, new JTextArea(addText));
    }

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

    public void editEntry() {
        int notfound = 0;
        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: "));
                break;
            } else {
                notfound++;
            }
        }
        if (notfound != 0) {
            JOptionPane.showMessageDialog(null, "Name Not Found!");
        }
        notfound = 0;
    }

    public void deleteEntry() {
        SName = JOptionPane.showInputDialog("Enter Name to delete: ");
        for (int i = 0; i < counter; i++) {
            if (entry[i].getName().equals(SName)) {
                JOptionPane.showMessageDialog(null, "Found!");
                entry[i] = null;
                break;
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:31:39+00:00Added an answer on May 20, 2026 at 6:31 am

    Assigning the values to null is going to be the easiest practice. If you’re really picky, you could resize the array, but that would be rather pointless. Just keep a separate size counter and decrement it each time you set something to null.

    Another reason you’re getting a null pointer exception is that you have to consider what’s happening when you’re replacing values in your array with null but still iterating by counter. You’re going to be left with holes in your array upon deletion. The first solution would be to bypass null values altogether, and just shift your array down (somewhat of an expensive operation). The second would be to alter your methods to take those null values into consideration. Example:

    
    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));

    }

    I don't have a compiler on this computer, so consider it more of psuedo-code. But the idea is that the counter is only keeping track of how many non-null values you have in your address book, and that these null values could be in random places of your array. I added the nonNull integer as a local counter to keep track of how many values you've encountered that aren't null (so you aren't forced to run through the entire address book). Then, I added the if statement to ensure that the value at entry[i] isn't a null value (trying to invoke getInfo() on a null value is what's giving you that error). Lastly, I added the if statement to break the loop if you've encountered all of the non-null values you have stored. Hope this helps. (Also it may be worth considering a LinkedList to eliminate the null values all together).

    Actually, for simplicity's sake, you probably are much better off using a LinkedList, unless you are required to use an array, since you would need to alter all of your methods to take null spaces in your array into account. Assuming you're familiar with LinkedLists of course.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize this perhaps a naive question but still I cant figure out how
I'm trying to start off using FBJS, and I can't figure this out. The
i am trying to set the htmlAttribute property but i cant figure out the
I'm building a questionnaire mvc webapp, and i cant figure out how to pass
I can't figure out why the following wont work, any ideas?? public interface IFieldSimpleItem
I can't figure out if this is a bug in Firefox or an anomaly
I cant figure out why my program is showing null pointer exception. Plz help
I randomly get this error, and I can't figure out a way to fix
I can't figure out a use case for being able to annotate interfaces in
I can't figure out how to define the default constructor (when it exists overloads)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.