¿Cómo soluciono este problema con Java en mi código?

programación


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

Lo que he probado:

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

    }
}

Solución 1

Este es uno de los problemas más comunes que nos plantean y también es el que menos estamos preparados para responder, pero usted está mejor preparado para responderlo usted mismo.

Permítanme explicarles lo que significa el error: ha intentado utilizar una variable, propiedad o valor de retorno de un método pero contiene un valor nulo, lo que significa que no hay ninguna instancia de una clase en la variable.
Es un poco como un bolsillo: tienes un bolsillo en la camisa, que usas para guardar un bolígrafo. Si mete la mano en el bolsillo y descubre que no hay un bolígrafo, no podrá firmar su nombre en una hoja de papel, ¡y obtendrá miradas muy divertidas si lo intenta! El bolsillo vacío le da un valor nulo (¡aquí no hay bolígrafo!), por lo que no puede hacer nada que normalmente haría una vez que recuperó su bolígrafo. ¿Por qué está vacío? Ésa es la pregunta: puede ser que hayas olvidado recoger el bolígrafo cuando saliste de casa esta mañana, o posiblemente lo hayas dejado en el bolsillo de la camisa de ayer cuando te lo quitaste anoche.

No podemos saberlo, porque no estábamos allí, y lo que es más importante, ¡ni siquiera podemos ver tu camisa y mucho menos lo que hay en el bolsillo!

Volviendo a las computadoras, usted ha hecho lo mismo de alguna manera, y no podemos ver su código, y mucho menos ejecutarlo y descubrir qué contiene nulo cuando no debería.
Pero puedes, y tu IDE te ayudará aquí. Ejecute su programa en el depurador y cuando falle, le mostrará la línea en la que encontró el problema. Luego puede comenzar a mirar las distintas partes para ver qué valor es nulo y comenzar a revisar su código para descubrir por qué. Así que coloque un punto de interrupción al principio del método que contiene la línea de error y ejecute su programa desde el principio nuevamente. Esta vez, el depurador se detendrá antes del error y le permitirá examinar lo que está sucediendo recorriendo el código mirando sus valores.

Pero no podemos hacer eso: no tenemos su código, no sabemos cómo usarlo si lo tuviéramos, no tenemos sus datos. Pruébelo y vea cuánta información puede encontrar.

Solución 2

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

コメント

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