So my code below is essentially a phone number directory that at first only displays 10 set names and numbers. If you enter one of their names, the corresponding number will appear! Then you have to enter in 20 more names and numbers after that. Finally, you are asked to enter a name at the end of the program (assuming it's not one of the original 10), however let's say I gave input for names[15], when I want to display it in the LAST for loop, or for any name I seem to enter, I receive an NPE right in that final for loop. Any idea why? I added two lines of code to display the array at names[15] and phoneNumbers[15] and that works (which are not in this program currently), so I'm just curious as to why I am receiving an NPE in the first place?
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);
}
}