मैं अपने कोड में जावा के साथ इस समस्या को कैसे ठीक करूं?


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

मैंने क्या प्रयास किया है:

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

    }
}

समाधान 1

यह हमारे द्वारा पूछे जाने वाली सबसे आम समस्याओं में से एक है, और यह ऐसी समस्या भी है जिसका उत्तर देने के लिए हम सबसे कम सक्षम हैं, लेकिन आप स्वयं उत्तर देने के लिए सबसे अधिक सक्षम हैं।

मैं बस यह समझाता हूं कि त्रुटि का क्या मतलब है: आपने एक वेरिएबल, प्रॉपर्टी, या एक विधि रिटर्न वैल्यू का उपयोग करने का प्रयास किया है लेकिन इसमें शून्य है – जिसका अर्थ है कि वेरिएबल में किसी वर्ग का कोई उदाहरण नहीं है।
यह कुछ हद तक एक जेब की तरह है: आपकी शर्ट में एक जेब होती है, जिसका उपयोग आप पेन रखने के लिए करते हैं। यदि आप जेब में हाथ डालते हैं और पाते हैं कि वहां कोई पेन नहीं है, तो आप कागज के टुकड़े पर अपना नाम हस्ताक्षर नहीं कर सकते – और यदि आप कोशिश करेंगे तो आपको बहुत अजीब लुक मिलेगा! खाली जेब आपको शून्य मान दे रही है (यहां कोई पेन नहीं है!) इसलिए आप ऐसा कुछ भी नहीं कर सकते जो आप अपना पेन वापस पाने के बाद सामान्य रूप से करते हैं। यह खाली क्यों है? यह सवाल है – हो सकता है कि आप आज सुबह घर से निकलते समय अपना पेन उठाना भूल गए हों, या संभवतः आपने कल रात जब कलम उतारी थी तो उसे कल की शर्ट की जेब में छोड़ दिया था।

हम नहीं बता सकते, क्योंकि हम वहां नहीं थे, और इससे भी महत्वपूर्ण बात यह है कि हम आपकी शर्ट भी नहीं देख सकते, जेब में क्या है यह तो दूर की बात है!

कंप्यूटर पर वापस जाएं, और आपने किसी तरह से वही काम किया है – और हम आपका कोड नहीं देख सकते हैं, इसे चलाने और यह पता लगाने की तो बात ही दूर है कि इसमें शून्य क्या है जबकि ऐसा नहीं होना चाहिए।
लेकिन आप कर सकते हैं – और आपकी आईडीई यहां आपकी मदद करेगी। अपने प्रोग्राम को डिबगर में चलाएं और जब यह विफल हो जाता है, तो यह आपको वह लाइन दिखाएगा जिस पर उसे समस्या मिली थी। फिर आप यह देखने के लिए इसके विभिन्न हिस्सों को देखना शुरू कर सकते हैं कि कौन सा मान शून्य है और इसका कारण जानने के लिए अपने कोड को वापस देखना शुरू कर सकते हैं। इसलिए त्रुटि रेखा वाली विधि की शुरुआत में एक ब्रेकपॉइंट लगाएं, और अपने प्रोग्राम को फिर से शुरू से चलाएं। इस बार, डिबगर त्रुटि से पहले रुक जाएगा, और आपको अपने मूल्यों को देखते हुए कोड के माध्यम से कदम उठाकर जांच करने देगा कि क्या हो रहा है।

लेकिन हम ऐसा नहीं कर सकते – हमारे पास आपका कोड नहीं है, हम नहीं जानते कि अगर हमारे पास है तो उसका उपयोग कैसे करें, हमारे पास आपका डेटा नहीं है। तो इसे आज़माएँ – और देखें कि आप कितनी जानकारी प्राप्त कर सकते हैं!

समाधान 2

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

コメント

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