System.out.println("Welcome to the Personal Contact Assistant!");
System.out.println("How can I help you?");
System.out.println("(add) (get) (quit)");
String option = input.nextLine();
Contact[] Contacts;
Contacts = new Contact[500];
int index = 0;
boolean finished = false;
while(finished==false){
switch (option) {
case "add":
System.out.println("You have selected add.");
Contact newContact = new Contact();//constructs new contact object called newContact
newContact.setNewInfo();//gathers input for newContact
System.out.println("Contact Will be Saved as:");
newContact.print();//prints gathered information
Contacts[index] = newContact;//saves contact to array
index++;//advances index
System.out.println("Can I do something else for you? (add) (get) (quit)");
option = input.nextLine();
break;
case "get":
System.out.println("You have selected get.");
System.out.println("Enter the contact's first name:");
String tempName;
tempName = input.nextLine();
for(int i=0; i<499; i++){
System.out.println(":"+i);
if(Contacts[i].getFirstName().equals(tempName)){
System.out.println("Contact Found:");
Contacts[i].print();}
}
System.out.println("Can I do something else for you? (add) (get) (quit)");
option = input.nextLine();
break;
これらは、私が取り組んでいる連絡先アプリの追加および取得セグメントです。My Contact クラスには、連絡先の名前、住所などを設定および取得するためのメソッドが含まれています。コンパイラ エラーは発生しませんが、プログラムの実行中に for ループで nullpointerexception エラーが発生します。さらに、出力は System.out.println(":"+i); から 1 つの数値のみを出力します。その名の連絡先が見つからない場合は、各連絡先の完全な連絡先情報が返され、エラーが発生します。ループを完了し、最初の名前との連絡先を出力してから、メインの外側のループに戻りたいだけです。ヘルプ?