Java 为什么这会引发空点异常?
出于某种原因,每当我测试它时,IDE都会在第46行抛出一个NullPointerException(检查数组名称的For循环)。它似乎可以工作,因为它获取名称并返回它,但随后立即出现异常。有人能解释一下吗?谢谢 导入java.util.*; 类别电话号码{Java 为什么这会引发空点异常?,java,arrays,nullpointerexception,Java,Arrays,Nullpointerexception,出于某种原因,每当我测试它时,IDE都会在第46行抛出一个NullPointerException(检查数组名称的For循环)。它似乎可以工作,因为它获取名称并返回它,但随后立即出现异常。有人能解释一下吗?谢谢 导入java.util.*; 类别电话号码{ private String name; private String number; PhoneNumbers(String n, String numb) { this.name = n; this.number = n
private String name;
private String number;
PhoneNumbers(String n, String numb) {
this.name = n;
this.number = numb;
}
public String getName() {
return name;
}
public String getNumber() {
return number;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean repeat = true;
int count = 11;
PhoneNumbers[] contacts = new PhoneNumbers[30];
contacts[0] = new PhoneNumbers("Gina", "(847) 342-0912");
contacts[1] = new PhoneNumbers("Marcia", "(847) 341-2392");
contacts[2] = new PhoneNumbers("Rita", "(847) 354-0654");
contacts[3] = new PhoneNumbers("Jennifer", "(414) 234-0912");
contacts[4] = new PhoneNumbers("Fred", "(414) 435-0434");
contacts[5] = new PhoneNumbers("Neil", "(608) 123-0914");
contacts[6] = new PhoneNumbers("Judy", "(608) 123-0312");
contacts[7] = new PhoneNumbers("Arlene", "(608) 123-0312");
contacts[9] = new PhoneNumbers("LaWanda", "(920) 787-9813");
contacts[10] = new PhoneNumbers("Deepak", "(930) 412-0991");
while (repeat) {
boolean found = false;
System.out.print("Enter your friends name: ");
String nameInput = sc.nextLine();
if (!nameInput.equalsIgnoreCase("quit")){
for(int i = 0; i < count - 1; i++){
if (contacts[i].getName().equalsIgnoreCase(nameInput)) {<---Here's where the issue occurs***
found = true;
System.out.println(contacts[i].getName() + "'s number is " + contacts[i].getNumber());
}
}
if (!found){
System.out.println("Enter your friends phone number: ");
String num = sc.nextLine();
contacts[count + 1] = new PhoneNumbers(nameInput, num);
count++;
}
}else if (nameInput.equalsIgnoreCase("quit")) {
repeat = false;
}else if (count == 30){
repeat = false;
}
}
}
}
私有字符串名称;
私有字符串编号;
电话号码(字符串n,字符串numb){
this.name=n;
这个数字=麻木;
}
公共字符串getName(){
返回名称;
}
公共字符串getNumber(){
返回号码;
}
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
布尔重复=真;
整数计数=11;
电话号码[]联系人=新电话号码[30];
联系人[0]=新电话号码(“吉娜”,“(847)342-0912”);
联系人[1]=新电话号码(“Marcia”,“(847)341-2392”);
联系人[2]=新电话号码(“丽塔”,“847”354-0654”);
联系人[3]=新电话号码(“Jennifer”,“(414)234-0912”);
联系人[4]=新电话号码(“Fred”,“(414)435-0434”);
联系人[5]=新电话号码(“Neil”,“(608)123-0914”);
联系人[6]=新电话号码(“朱迪”,“608”123-0312”);
联系人[7]=新电话号码(“爱琳”,“608”123-0312”);
联系人[9]=新电话号码(“LaWanda”,“(920)787-9813”);
联系人[10]=新电话号码(“Deepak”,“(930)412-0991”);
while(重复){
布尔值=false;
System.out.print(“输入您的朋友姓名:”);
字符串nameInput=sc.nextLine();
如果(!nameInput.equalsIgnoreCase(“退出”)){
对于(int i=0;i 如果(contacts[i].getName().equalsIgnoreCase(nameInput)){您在初始化过程中缺少contacts[8]
,那么在尝试访问不存在的联系人8的名称时,您将获得NullPointerException
:
// ...
contacts[7] = new PhoneNumbers("Arlene", "(608) 123-0312");
contacts[9] = new PhoneNumbers("LaWanda", "(920) 787-9813");
// ...
Debug会比其他人更好地回答您的问题。查看java.util.List,而不是像您那样使用数组。它将帮助您处理这些错误。而且您的循环不会在整个数组上迭代,因为它是在i