2012-04-19 25 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行目ですか? –

答えて

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.
uは、この呼び出し: - オブジェクト場合

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

nullよりも、uを呼び出すと明らかにuが得られるよりも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]; 

これは、すべての要素がnullに初期化されるcontactsサイズのアレイを作成します。

内部の任意の要素にアクセスしようとすると(Test[0])、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); 
} 

:あなたのセッターがゲッターとして命名されている理由は?

+0

それは私が++であってはいけませんか? –

+0

実際には違いはありません。ステートメントは独立して実行されるため、副作用はありません。 – Jack

関連する問題