我究竟做错了什么?

时间:2012-04-19 04:21:17

标签: java

我只是想制作一个简单的“电话簿”应用,但我做错了。但是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)

我知道我的方法调用,但我无法弄清楚我做错了什么我已经尝试了几次不同的迭代但仍然没有骰子。

2 个答案:

答案 0 :(得分: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);
}

出于好奇:为什么你的二传手被命名为吸气剂?

答案 1 :(得分: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

所以现在如何删除此问题 -

你得到名称电话号码,然后创建一个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)