是什么导致了NPE错误?

时间:2017-04-05 16:16:50

标签: java arrays null

所以下面我的代码本质上是一个电话号码目录,最初只显示10个设置名称和数字。如果输入其中一个名称,将显示相应的数字!然后你必须输入20多个名字和数字。最后,你被要求在程序的最后输入一个名字(假设它不是原来的10个名字),但是当我想在LAST for循环中显示时,让我说我给了名字输入[15],或者对于我似乎输入的任何名称,我在最后的循环中获得了NPE权利。知道为什么吗?我添加了两行代码来显示名称[15]和phoneNumbers [15]的数组,并且有效(当前不在此程序中),所以我只是好奇为什么我在第一次接收NPE放置?

import java.util.Scanner;

public class PhoneNumbers 
{
////////////////////////////////////////////////// Main should do all the work w/the help of if statements and loops
public static void main(String[] args)
{
    ////////////////////////////////////////////// Arrays declared
    String[] names = new String[30];
    long[] phoneNumbers = new long[30];
    ////////////////////////////////////////// Scanner object made
    Scanner input = new Scanner(System.in);

    names[0] = "George";
    names[1] = "Charles";
    names[2] = "Louie";
    names[3] = "Blu";
    names[4] = "Master Chief";
    names[5] = "Mark";
    names[6] = "Sarge";
    names[7] = "Echo";
    names[8] = "Kat";
    names[9] = "Red";


    phoneNumbers[0] = 6268507090L;
    phoneNumbers[1] = 6268507091L;
    phoneNumbers[2] = 6268507092L;
    phoneNumbers[3] = 6268507093L;
    phoneNumbers[4] = 6268507094L;
    phoneNumbers[5] = 6268507095L;
    phoneNumbers[6] = 6268507096L;
    phoneNumbers[7] = 6268507097L;
    phoneNumbers[8] = 6268507098L;
    phoneNumbers[9] = 6268507099L;
    /////////////////////////////////////////////////////////////// Loops to display all names
    System.out.println("Names in our directory:");
for(int x = 0; x < 10; ++x)
{
    System.out.println(names[x]);
}

///////////////////////////////////////////////////////////////////////////////////// Displays associated name and #
String name;
    System.out.println("Please enter the name for the associated phone number.");
    name = input.nextLine();
    for(int x = 0; x < 10; ++x)
    {
        if(names[x].equals(name))
        {
            System.out.println(names[x]+ "'s number is: " +phoneNumbers[x]);
        }
    }
    /////////////////////////////////////////////// Allows the closing of the program

    ///////////////////////////////////////////////////////////////////////// Creates another name and # in arrays
for(int z = 0; z < 19; ++z)
{
    boolean newEntry = false;
    String newName;

    newName = getName();
    if(newName.equals("EXIT"))
    {
        System.out.println("ENDED");
        endProgram();
    }
        for (int x = 0; x < 10; ++x)
        {
            if(names[x].equals(newName))
                System.out.println("This name has already been entered!");
            newEntry = true;
        }
        if(newEntry)
            {
            System.out.println("Name not found! Adding to the directory!");
            long newNumber;
            int y = 10;

            ++ y;
            names[z+y] = newName;
            System.out.println("Entry " + (z+y) + " has now been added to the directory.");
            System.out.println("Please enter their phone number: ");
            newNumber = input.nextLong();
             phoneNumbers[z+y] = newNumber;
            System.out.println(names[z+y] + "'s Phone number, " + "Position "+ (y + z) + " :" + phoneNumbers[z+y]);

            }




}
{
    System.out.println("Directory full!");
    System.out.println("Directory/Array size: " + names.length + " going from 0-29!");

    String nameNow;
    nameNow = getName2();
    for(int x = 0; x < names.length; ++x)
    {
        if(names[x].equals(nameNow))
        {
            System.out.println(names[x]+ "'s number is: " +phoneNumbers[x]);
        }
    }

}

}

public static String getName()
{
    Scanner input = new Scanner(System.in);
    String anotherName;
    System.out.println("Enter EXIT now if you'd like to close the program. \nOtherwise, please type another name.");
    anotherName = input.nextLine();
    return anotherName;
}
public static String getName2()
{
    Scanner input = new Scanner(System.in);
    String anotherName;
    System.out.println("Please type a name to see the number associated with them.");
    anotherName = input.nextLine();
    return anotherName;
}

public static void endProgram()
{
    System.exit(0);
}
}

0 个答案:

没有答案