gpt4 book ai didi

java - 来自空数组单元的 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 22:32:23 25 4
gpt4 key购买 nike

使用Java而不是IDE,而是使用命令行,我应该编写一个基本的电话簿程序,重点关注数组,其中用户有三个选项:在数组中搜索名称并打印名称和电话号码(如果找到姓名),则根据用户输入将新姓名和电话号码添加到数组中,并根据用户输入从数组中删除姓名和电话号码。添加功能完全正常工作,但搜索和删除功能无法正常工作。我的代码可以编译并运行,但每当我尝试搜索或删除数组中不存在的名称时,都会收到 NullPointerException 运行时错误。

这些是程序“删除条目”部分的具体说明:

“要从这样的数组中删除姓名和号码,首先找到该姓名的单元格。如果要删除的姓名不在数组中,则报告错误。现在将引用复制到最后一个非空单元格中到删除的名字的单元格。将最后一个非空单元格设置为空。现在删除的PhoneEntry是垃圾,并且所有数组的末尾仍然有所有空值。”

这是我的代码:

import java.util.*;

class PhoneEntry
{
String name; // name of a person
String phone; // their phone number

PhoneEntry( String n, String p )
{
name = n; phone = p;
}
}

class PhoneBook
{
PhoneEntry[] phoneBook;

PhoneBook() // constructor
{
phoneBook = new PhoneEntry[ 10 ] ;

phoneBook[0] = new PhoneEntry( "James Barclay", "(418) 665-1223" );
phoneBook[1] = new PhoneEntry( "Grace Dunbar", "(860) 399-3044" );
phoneBook[2] = new PhoneEntry( "Paul Kratides", "(815) 439-9271" );
phoneBook[3] = new PhoneEntry( "Violet Smith", "(312) 223-1937" );
phoneBook[4] = new PhoneEntry( "John Wood", "(913) 883-2874" );
phoneBook[5] = new PhoneEntry( null, null );
phoneBook[6] = new PhoneEntry( null, null );
phoneBook[7] = new PhoneEntry( null, null );
phoneBook[8] = new PhoneEntry( null, null );
phoneBook[9] = new PhoneEntry( null, null );
}

PhoneEntry search( String targetName )
{
for ( int i = 0 ; i < phoneBook.length ; i++ )
{
System.out.println(i); // debug
if ( phoneBook[i] != null && phoneBook[i].name.equals( targetName ) )
{
return phoneBook[i];
}
}
return null;
}

PhoneEntry addEntry( String addName, String addNumber )
{
boolean found = false;
for ( int i = 0 ; i < phoneBook.length ; i++ )
{
System.out.println(i); // debug
if ( phoneBook[i].name == null )
{
phoneBook[i] = new PhoneEntry( addName, addNumber );
found = true;
break;
}
}
if ( !found )
{
System.out.println("Phone book is full! Delete an entry first!");
}
return null;
}

PhoneEntry deleteEntry( String deleteName )
{
boolean found = false;
for ( int i = 0 ; i < phoneBook.length ; i++ )
{
System.out.println(i); // debug
if ( phoneBook[i] != null && phoneBook[i].name.equals( deleteName ) )
{
System.out.println( phoneBook[i].name + " found"); // debug
phoneBook[i] = new PhoneEntry( null, null );
found = true;
break;
}
}
if ( !found )
{
System.out.println( "Entry not found." );
}
return null;
}
}

class PhoneBookComplete
{
public static void main ( String[] args )
{
PhoneBook pb = new PhoneBook();
Scanner scan = new Scanner( System.in );
String tempName, tempNumber, tempDelName;

// INITIAL WELCOME MESSAGE START

System.out.println();
System.out.println("**********************");
System.out.println("***** PHONE BOOK *****");
System.out.println("**********************");
System.out.println();
System.out.println(" 1 Search for an entry");
System.out.println(" 2 Add a new entry");
System.out.println(" 3 Delete an entry");
System.out.println(" 4 Quit program");
System.out.println();
System.out.print("Enter a command: ");

String userInput = scan.nextLine();
PhoneEntry entry;
System.out.println();

// INITIAL WELCOME MESSAGE END

while ( userInput != "quit" )
{
if ( userInput.equals("1") ) // NAME SEARCH
{
System.out.println(" NAME SEARCH START");
System.out.println("**********************");
System.out.println("***** NAME SEARCH ****");
System.out.println("**********************");
System.out.println();
System.out.print("Enter a name: ");
userInput = scan.nextLine();
entry = pb.search( userInput );

if ( entry != null )
{
System.out.println();
System.out.println( " " + entry.name + ": " + entry.phone );
System.out.println();
}
else if ( userInput.equals("quit") )
{
break;
}
else
{
System.out.println("Name not found.");
System.out.println();
}
System.out.println(" NAME SEARCH END");
System.out.println();
}
else if ( userInput.equals("2") ) // ADD ENTRY
{
System.out.println(" ADD ENTRY START");

System.out.println("**********************");
System.out.println("***** ADD ENTRY ******");
System.out.println("**********************");
System.out.println();

System.out.print("Enter full name: ");
tempName = scan.nextLine();

if ( userInput.equals("quit") )
{
break;
}

System.out.print("Enter phone number: ");
tempNumber = scan.nextLine();

if ( userInput.equals("quit") )
{
break;
}

entry = pb.addEntry( tempName, tempNumber );

System.out.println();
System.out.println( "Entry for " + tempName + " successfully added." );
//System.out.println("Phone book is full! Delete an entry first!");

System.out.println(" ADD ENTRY END");
System.out.println();
}
else if ( userInput.equals("3") ) // DELETE ENTRY
{
System.out.println(" DELETE ENTRY START");

System.out.println("**********************");
System.out.println("**** DELETE ENTRY ****");
System.out.println("**********************");
System.out.println();

System.out.print("Enter full name: ");
tempDelName = scan.nextLine();
entry = pb.deleteEntry( tempDelName );

if ( entry == null )
{
System.out.println();
System.out.println( "Entry for " + tempDelName + " successfully deleted." );
System.out.println();
}
else if ( userInput.equals("quit") )
{
break;
}
else
{
System.out.println();
}

System.out.println(" DELETE ENTRY END");
System.out.println();
}
else if ( userInput.equals("4") ) // QUIT PROGRAM
{
System.out.println();
break;
}

//System.out.println("Select a command:");
System.out.println("**********************");
System.out.println("**********************");
System.out.println("**********************");
System.out.println();
System.out.println(" 1 Search for an entry");
System.out.println(" 2 Add a new entry");
System.out.println(" 3 Delete an entry");
System.out.println(" 4 Quit program");
System.out.println();
System.out.print("Enter a command: ");
userInput = scan.nextLine();
}
System.out.println("Goodbye.");
}
}

以下是当我尝试搜索不存在的名称时出现的错误:

Exception in thread "main" java.lang.NullPointerException
at PhoneBook.search(PhoneBookComplete.java:39)
at PhoneBookComplete.main(PhoneBookComplete.java:128)

这是当我尝试删除不存在的名称时出现的错误:

Exception in thread "main" java.lang.NullPointerException
at PhoneBook.deleteEntry(PhoneBookComplete.java:73)
at PhoneBookComplete.main(PhoneBookComplete.java:193)

我知道这与我使用 null 有关,但是除了 null 我还能用什么来代替呢?另外,我应该无论如何都使用null。

所以我想我的问题是:应该在我的代码中修改什么,以便当用户尝试删除或搜索不存在的名称时,打印错误消息而不使程序崩溃?

在回答时,请记住我对 Java 还很陌生,数组、方法之类的东西对我来说仍然很困惑。如果需要更多信息等,请询问,您将收到。话虽如此,任何帮助和解释都值得赞赏和欢迎。谢谢!

最佳答案

检查 name 是否不为 null,这

if (phoneBook[i] != null && phoneBook[i].name.equals(targetName))

应该是这样的

if (phoneBook[i] != null && targetName.equals(phoneBook[i].name))

deleteEntry类似

if (phoneBook[i] != null && deleteName.equals(phoneBook[i].name))

因为当phoneBook[i].namenull时,它会导致NullPointerException调用.equals() 关于它。您还可以在 .name 上添加 null 测试,但随后您需要另一个 && ,例如

if (phoneBook[i] != null && phoneBook[i].name != null &&
phoneBook[i].name.equals(targetName))

关于java - 来自空数组单元的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27476901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com