Làm cách nào để khắc phục sự cố này với java trong mã của tôi

lập trình


I'm currently running into a nullPointerException when I try to search the address book. Also, running into an issue printing the contents of the ArrayList addressbook. Any tips on how I can clean up the code // split it up into different files, would appreciate that feedback

Những gì tôi đã thử:

import java.util.*;

public class addressBook {

    private String firstName;
    private String lastName;
    private String phoneNumber;
    private String email;
    static Scanner input = new Scanner(System.in);

    public void addressBookEntry(String firstName, String lastName, String phoneNumber, String email){
        this.firstName = firstName;
        this.lastName = lastName;
        this.phoneNumber = phoneNumber;
        this.email = email;
    }

    public String getFirstName() {
	    return firstName;
	}

	public void setFirstName(String firstName) {
	    this.firstName = firstName;
	}

	public String getLastName() {
	    return lastName;
	}

	public void setLastName(String lastName) {
	    this.lastName = lastName;
	}

	public String getPhoneNumber() {
	    return phoneNumber;
	}

	public void setPhone(String phoneNumber) {
	    this.phoneNumber = phoneNumber;
	}

	public String getEmail() {
	    return email;
	}

	public void setEmail(String email) {
	    this.email = email;
	}

    // Create an arraylist of entry objects
    static ArrayList<addressBookEntry> Book = new ArrayList<addressBookEntry>();

    // public void addressBookEntry(){
    //     System.out.println("This is the display method of superclass addressBookEntry");
    // }

    // Main menu for address book
    public static void printMenu(){
        
        int choice;

        System.out.println("1) Add an entry\n" +
                            "2) Remove an entry\n" +
                            "3) Search for a specific entry\n" +
                            "4) Print Address Book\n" +
                            "5) Delete Book\n" +
                            "6) Quit\n"+
                            "Please choose what you'd like to do with the database: ");

        // Assign choice variable to user input 
        choice = input.nextInt();
        if (choice == 1){
            addEntry();
        }
            else if (choice == 2){
                removeEntry();
            }
            else if (choice == 3){
                searchBook();
            }
            else if (choice == 4){
                printBook();
            }
            else if (choice == 5){
                deleteBook();
            }
            else if (choice == 6){
                quitBook();
            }
        //     // If choice != ints 1-6, reset and ask user for choice again.
            else {
                System.out.println("Invalid entry! Please try again.");
                printMenu();
            }
    }

    // Allows user to add an entry
    public static void addEntry(){
       Scanner entry = new Scanner(System.in);
            System.out.println("Please enter the following:");
                System.out.println("First Name: ");
                String firstName = entry.nextLine();
                System.out.println("Last Name: ");
                String lastName = entry.nextLine();
                System.out.println("Phone number: ");
                String phoneNumber = entry.nextLine();
                System.out.println("Email address: ");
                String email = entry.nextLine();
            
            Book.add(new addressBookEntry(firstName, lastName, phoneNumber, email));
        

        System.out.println("\nAdded new entry!\n");
        printMenu();
        
    }

    // Allows user to remove an entry
    public static void removeEntry(){
        Scanner removeEmail = new Scanner(System.in);
        System.out.println("Enter an entry's email to remove");
        String remove = removeEmail.nextLine();
        for (int i = 0; i<Book.size(); i++){
            if (Book.get(i).getEmail() == remove){
                System.out.println("Deleted the following entry:");
                System.out.println("****************************");
                System.out.println(Book.get(i));
                System.out.println("****************************");
                Book.remove(i);
            }
        }
        printMenu();
    }

    // // Allows user to search for an entry
    public static void searchBook(){
        
        System.out.println("Search address book: \n");
        System.out.println("1) First Name\n" +
                            "2) Last Name\n" +
                            "3) Phone Number\n" +
                            "4) Email Address\n" +
                            "Choose a search type: \n");
        int choice = input.nextInt();
        if (choice == 1){
            System.out.println("Please enter First Name: \n");
            String firstName = input.nextLine();
            for (int i = 0; i < Book.size(); i++) {
                if (Book.get(i).getFirstName().equals(firstName)) {
                    System.out.println(Book.get(i));
                    printMenu();
                }
            }
        }
            else if (choice == 2){
                System.out.println("Please enter Last Name: \n");
                String lastName = input.nextLine();
                for (int i = 0; i < Book.size(); i++) {
                    if (Book.get(i).getLastName().equals(lastName)) {
                        System.out.println(Book.get(i));
                        printMenu();
                    }
                }
            }
            else if (choice == 3){
                System.out.println("Please enter phone number: \n");
                String phone = input.nextLine();
                for (int i = 0; i < Book.size(); i++) {
                    if (Book.get(i).getPhoneNumber().equals(phone)) {
                        System.out.println(Book.get(i));
                        printMenu();
                    }
                }
            }
            else if (choice == 4){
                System.out.println("Please enter email: \n");
                String email = input.nextLine();
                for (int i = 0; i < Book.size(); i++) {
                    if (Book.get(i).getEmail().equals(email)) {
                        System.out.println(Book.get(i));
                        printMenu();
                    }
                }
            }
        
    }

    // Print out all entries in address book
    public static void printBook(){
      
        if(Book.size() == 0){
            System.out.println("Address book is empty!\n");
            printMenu();
        }
        for(int i = 0; i<Book.size(); i++){
            System.out.println("****************************");
            addressBookEntry curr = Book.get(i);
            System.out.println(curr.toString());
            System.out.println("****************************");
        }
        printMenu();
    }

    // // Remove all entries in address book
    public static void deleteBook(){
        Book.clear();
        System.out.println("Address book cleared!\n");
        printMenu();
    }

    // // Allows user to quit program
    public static void quitBook(){
        System.out.println("Quit book");
    }

    public static void main (String[] args){

        printMenu();
       

    }
}

Giải pháp 1

Đây là một trong những vấn đề chúng tôi thường gặp nhất và cũng là vấn đề chúng tôi ít có khả năng giải đáp nhất, nhưng bạn lại có đủ khả năng để tự trả lời.

Hãy để tôi giải thích ý nghĩa của lỗi: Bạn đã cố gắng sử dụng một biến, thuộc tính hoặc một giá trị trả về của phương thức nhưng nó chứa null – có nghĩa là không có phiên bản nào của một lớp trong biến đó.
Nó hơi giống một cái túi: bạn có một cái túi trong áo sơ mi, bạn dùng để đựng bút. Nếu bạn thò tay vào túi và thấy không có cây bút nào ở đó, bạn không thể ký tên mình lên một tờ giấy – và bạn sẽ nhận được những cái nhìn rất buồn cười nếu cố gắng! Chiếc túi trống sẽ mang lại cho bạn một giá trị null (không có bút ở đây!) Vì vậy bạn không thể làm bất cứ điều gì mà bạn thường làm sau khi lấy lại chiếc bút của mình. Tại sao nó lại trống rỗng? Đó chính là câu hỏi – có thể là sáng nay bạn quên lấy bút khi ra khỏi nhà, hoặc có thể bạn đã để quên bút trong túi áo sơ mi hôm qua khi cởi nó ra tối qua.

Chúng tôi không thể biết, bởi vì chúng tôi không ở đó, và quan trọng hơn nữa, chúng tôi thậm chí không thể nhìn thấy áo sơ mi của bạn, chứ đừng nói đến những gì trong túi!

Quay lại với máy tính, và bằng cách nào đó, bạn đã làm điều tương tự – và chúng tôi không thể nhìn thấy mã của bạn, chứ đừng nói đến việc chạy nó và tìm ra cái gì chứa null khi không nên.
Nhưng bạn có thể – và IDE của bạn sẽ giúp bạn ở đây. Chạy chương trình của bạn trong trình gỡ lỗi và khi nó bị lỗi, nó sẽ hiển thị cho bạn dòng mà nó phát hiện ra sự cố. Sau đó, bạn có thể bắt đầu xem xét các phần khác nhau của nó để xem giá trị nào là null và bắt đầu xem lại mã của mình để tìm hiểu lý do. Vì vậy, hãy đặt một điểm dừng ở đầu phương thức chứa dòng lỗi và chạy lại chương trình của bạn từ đầu. Lần này, trình gỡ lỗi sẽ dừng trước khi xảy ra lỗi và cho phép bạn kiểm tra điều gì đang xảy ra bằng cách duyệt qua mã, xem xét các giá trị của bạn.

Nhưng chúng tôi không thể làm điều đó – chúng tôi không có mã của bạn, chúng tôi không biết cách sử dụng nó nếu chúng tôi có nó, chúng tôi không có dữ liệu của bạn. Vì vậy hãy thử nó – và xem bạn có thể tìm hiểu được bao nhiêu thông tin!

Giải pháp 2

https://Www.semfirms.com/goto?url=https://halloween-pumpkin.blogspot.com/2023/11/Romantic-Pumpkin-Carving-Ideas.html

コメント

タイトルとURLをコピーしました