1

3 가지 클래스가 있습니다 : Main, ContactLibrary 및 ContactInfo. ContactLibrary에는 myPhoneBook이라는 ArrayList가 있습니다. ContactInfo는 이름, 주소 등을 포함하는 일련의 문자열로 구성됩니다.직렬화 가능 객체에 대한 입력을 얻으려고 할 때 NullPointerException이 발생했습니다.

사용자는 예를 들어 이름이나 입력과 관련된 모든 것을 검색하려고합니다. 입력은 ContactLibrary 클래스와 ContactInfo 클래스 (직렬화 가능 객체 모두)에서 수행됩니다.

그러나 그 지점에 도달하면 NPE 오류가 발생합니다.

You have 3 entry(s) saved to disc. 

Hello, and welcome to Team 6's contact list. 
What would you like to do? 
Enter the corresponding number of choice. 

1: Add an entry to the contact list. 
2: Print the entire contact list. 
3: Search for a contact. 
4: Exit the program. 

Please enter a number from 1-4. 
3 
What would you like to search by? 
Exception in thread "main" java.lang.NullPointerException 
    at ContactLibrary.searchByCriteria(ContactLibrary.java:62) 
    at Main.optionsPrompt(Main.java:62) 
    at Main.main(Main.java:25) 
1: Last Names. 
2: Emails. 
3: Zip codes. 

어떻게해야합니까? 여기

은 내 홈페이지 : http://ideone.com/uvfK4U 여기 을 (맨 의견에 다른 두 개의 클래스를 포함합니다)를 UML 다이어그램이다 : http://imgur.com/9W3TS

ContactLibrary 클래스의 요청에 따라 :

/** 
* ContactLibrary, when constructed, creates an ArrayList of ContactLibrary 
* references called myPhoneBook. Every index is made to fill in objects 
* of ContactInfo, which contains entries and credentials. 
* 
* Contains methods to create a new entry, search and print by criteria, and print list. 
*/ 
import java.util.*; 

public class ContactLibrary implements java.io.Serializable { 
    private static final long serialVersionUID = 1L; 
    private ArrayList<ContactInfo> myPhoneBook; 
    private Scanner libraryInput = new Scanner(System.in); 

    /** Constructs the ArrayList that will hold references to ContactInfo. */ 
    public ContactLibrary() { 
     myPhoneBook = new ArrayList<ContactInfo>(); 
    } 

    /** 
    * Adds an entry to the ArrayList and utilizes the set methods in 
    * ContactInfo. 
    */ 
    public void addEntry() { 
     int doAgain = 1; 
     do { 
      myPhoneBook.add(new ContactInfo()); 
      System.out.println("Would you like to enter another contact?"); 
      System.out.println("1: Yes."); 
      System.out.println("2: No."); 
      doAgain = libraryInput.nextInt(); 
     } while (doAgain == 1); 
    } 

    /** 
    * Goes through every index in myPhoneBook and runs ArrayList.get() on them. 
    */ 
    public void printList() { 
     for (int i = 0; i < myPhoneBook.size(); i++) { 
      System.out.println(myPhoneBook.get(i)); 
     } 
    } 

    /** Counts the number of objects within myPhoneBook and returns a string. */ 
    public String scanDisc() { 
     int entryCount = myPhoneBook.size(); 
     return "You have " + entryCount + " entry(s) saved to disc.\n"; 
    } 

    /** 
    * The prompt for having the user search the database via criteria. Asks the 
    * user to enter in their search criteria. 
    */ 
    public void searchByCriteria() { 
     String criteria; 
     //libraryInput = new Scanner("System.in"); 
     int subSubMenuChoice = 0; 
     System.out.println("What would you like to search by?"); 
     System.out.println("1: Last Names."); 
     System.out.println("2: Emails."); 
     System.out.println("3: Zip codes."); 
     subSubMenuChoice = libraryInput.nextInt(); 
     switch (subSubMenuChoice) { 
     case 1: 
      System.out 
        .println("Please enter the last name you'd like to search for:"); 
      criteria = libraryInput.next(); 
      searchByLastName(criteria); 
      break; 
     case 2: 
      System.out 
        .println("Please enter the e-mail you'd like to search for:"); 
      criteria = libraryInput.next(); 
      searchByEmail(criteria); 
      break; 
     case 3: 
      System.out 
        .println("Please enter the zip code you'd like to search for:"); 
      criteria = libraryInput.next(); 
      searchByZip(criteria); 
      break; 
     default: 
      System.out.println("Exiting"); 
      break; 
     } 
    } 

    /** 
    * Loops through every element in the array and returns a toString of that 
    * index for comparing with the search criteria via contains(). 
    */ 
    public void searchByEmail(String criteria) { 
     for (int i = 0; i < myPhoneBook.size(); i++) { 
      if (criteria.compareTo((myPhoneBook.get(i)).getEmail()) == 0) { 
       System.out.println(myPhoneBook.get(i)); 
      } else { 
       System.out.print(""); 
      } 
     } 
    } 

    /** 
    * Loops through every element in the array and returns a toString of that 
    * index for comparing with the search criteria via contains(). 
    */ 
    public void searchByLastName(String criteria) { 
     for (int i = 0; i < myPhoneBook.size(); i++) { 
      if (criteria.compareTo((myPhoneBook.get(i)).getLastName()) == 0) { 
       System.out.println(myPhoneBook.get(i)); 
      } else { 
       System.out.print(""); 
      } 
     } 
    } 

    /** 
    * Loops through every element in the array and returns a toString of that 
    * index for comparing with the search criteria via contains(). 
    */ 
    public void searchByZip(String criteria) { 
     for (int i = 0; i < myPhoneBook.size(); i++) { 
      if (criteria.compareTo((myPhoneBook.get(i)).getZipcode()) == 0) { 
       System.out.println(myPhoneBook.get(i)); 
      } else { 
       System.out.print(""); 
      } 
     } 
    } 

    /** Reorganizes the array in order by last name. */ 
    public void sortData() { 
     Collections.sort(myPhoneBook); 
    } 

} 
+1

클래스의 코드를 게시하시기 바랍니다 (수업 ContactLibrary에) :

myLibrary.searchByCriteria(menuInput); 

이 줄을 삭제 거기에 ... 메서드 searchByCriteria() – Frank

+1

에 stacktrace를 게시하십시오. 스레드에서 –

+0

예외 Main.main (Main.java:25)에서 Main.optionsPrompt (Main.java:62) 에서 ContactLibrary.searchByCriteria (ContactLibrary.java:62) 에서 "주요"java.lang.NullPointerException이 는 인가 스택 트레이스가 아닌가? –

답변

0

당신이 만드는 같은 InputStream에있는 복수의 Scanner 오브젝트. 사용하는 메소드 나 클래스의 constructor의 다음의 클래스에 1 개만 작성해, 그것을 전달하는 것이 최상입니다.

동일한 스트림에서 여러 스캐너 사용. 스캐너는 스트림을 소비 할 수 있으며, 예상되는 부작용을 유발할 수 있습니다. 그것을하지 않는 것이 좋습니다.

당신이 당신의 메인 클래스의 라인 (65)을 대체 할 수있는 다음 nullpointer 그대로 ContactLibrary :

private Scanner libraryInput = new Scanner(System.in); 
+0

그래서 메인 클래스에 하나의 스캐너 만 설치해야한다고 말하고 있습니다 ... 다른 두 클래스와 어떻게해야합니까? 로컬 스캐너를 만드시겠습니까? –

+0

아니 통과, 내 대답을 편집 ... – Frank

+0

아! 나는 지금 본다. 내 메서드는이를 잡기 위해 스캐너 매개 변수를 포함해야하고 메서드 자체 내에서 사용할 수 있습니다. 정말 고맙습니다! –

관련 문제