私は単純な "電話帳"アプリを作ろうとしていますが、何か間違っています。しかし、何を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)
私はその私のメソッド呼び出しを知っているが、私は、私はいくつかの異なる回の反復が、まだありませんサイコロを試してみた間違ってやっているかを把握することはできません。
どの行が31行目ですか? –