2012-04-19 279 views
-1

我只是想做一個簡單的「電話簿」應用程序,但我做錯了什麼。但是idk是什麼。我在做什麼錯了?

這是我的第一課,

import java.util.Scanner; 
    public class PhoneBookEntryDemo 
    { 
    public static void main(String[] args){ 
     int k=0,contacts=0; 
     String position; 
     Scanner KB = new Scanner(System.in); 

     System.out.println("This is a automatic phonebook. the first of its kind."); 
     System.out.println("How many contacts do you want to enter today?"); 
     contacts = KB.nextInt(); 
     PhoneBookEntry[] Test = new PhoneBookEntry[contacts]; 
     do{ 
       switch (k) {  //this is for formatting the out put 
       case 0: position="st"; 
         break; 
       case 1: position="nd"; 
         break; 
       case 2: position="rd"; 
         break; 
       default: position="th"; 
         break; 
      } 
      System.out.println("Please enter the name "+ (k+1)+position+" of the contact: "); 
      Test[k].getName(KB.next()); //sets the name of what ever the counter is @ 
      System.out.println("Now enter the phone number: "); 
      Test[k].getPhoneNumber(KB.nextInt()); //sets the phone number at whatever the counter is @ 
      k++; 
     }while(k<contacts); 
     } 
    } 

這是我第二類,

public class PhoneBookEntry 
    { 
     String name; 
     int phoneNumber; 
     public PhoneBookEntry(String aName, int aPhoneNumber){ 
      name = aName; 
      phoneNumber = aPhoneNumber; 
     } 
     public void getName(String setName){ 
      name = setName; 
     } 
     public void getPhoneNumber(int setPhoneNumber){ 
      phoneNumber = setPhoneNumber; 
     } 

    } 

它符合,但它拋出一個運行時錯誤。

java.lang.NullPointerException at PhoneBookEntryDemo.main(PhoneBookEntryDemo.java:31) 

我知道它我的方法調用,但我想不出什麼我做錯了,我嘗試了幾種不同的迭代,但仍然沒有骰子。

+6

哪條線是31號線? – 2012-04-19 04:23:27

回答

0

的問題很簡單..

PhoneBookEntry[] Test = new PhoneBookEntry[contacts]; 

這是PhoneBookEntry類型的數組... - they're just compiler syntactic sugar for specific classes. The JVM has no knowledge of them. That means the default value for the type is null.
這之後ü稱之爲: -

Test[k].getName(KB.next()); //sets the name of what ever the counter is @ 

如果一個對象是null比如果你打電話的任何方法,那麼你顯然會得到java.lang.NullPointerException

那麼現在怎麼刪除該Problem--:

u得到兩個電話號碼,然後創建的PhoneBookEntry class這些一個對象被要求怎麼把你們班有沒有default constructor像這樣

 System.out.println("Please enter the name "+ (k+1)+position+" of the contact: "); 
     String name=KB.next(); //sets the name of what ever the counter is @ 
     System.out.println("Now enter the phone number: "); 
     Int phone=Integer.parseInt(KB.next());; //coz your class take int 
     // now create your object 
     Test[k]=new PhoneBookEntry(name,phone) 
2
PhoneBookEntry[] Test = new PhoneBookEntry[contacts]; 

這產生contacts大小,其中每個元素被初始化爲null的陣列。

如果您嘗試訪問任何內部元素(例如Test[0]),您將獲得null。你不能在null上調用任何方法(就像你在使用getName(..))。

你應該遍歷數組的初始化和每一個元素,如

for (int i = 0; i < Test.length; ++i) 
    Test[i] = new PhoneBookEntry(name, phoneNumber); 

for (int i = 0; i < Test.length; ++i) 
{ 
    Test[i] = new PhoneBookEntry(); 
    Test[i].setName(name); 
    Test[i].setPhoneNumber(phoneNumber); 
} 

只是出於好奇:爲什麼你的setter方法被命名爲干將?

+0

不應該是我++嗎? – 2012-04-19 04:32:37

+0

實際上沒有區別,因爲聲明是獨立執行的,所以不會有任何副作用。 – Jack 2012-04-19 05:13:12