2012-04-19 225 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