每当我不给我的一个电话号码(null)赋值并使用当前代码块将其打印到屏幕时,它会给我一个NullPointerException。我不想在阅读之前用联系人填写我的“电话簿”中的每个点,因为它只是方式太多而无法输入。
这是抛出异常的代码:
for (int i = 0; i< array1.length; i++) {
num++;
System.err.println("Contact: " + num);
System.out.print(array1[counterB2][counterB]);
counterB++;
System.out.print(" " + array1[counterB2][counterB]);
counterB++;
String[] phoneNumArr= {
array1[counterB2][2].substring(0, 3),
array1[counterB2][2].substring(3,6),
array1[counterB2][2].substring(6)};
System.out.println(" ");
if (!array1[counterB2][2].equals(null)) {
System.out.println(phoneMsgFmt.format(phoneNumArr));
counterB = 0;
counterB2++;
}
}
任何帮助改善这一点以使其正常工作将不胜感激。
以下是代码的其余部分:
import java.util.Scanner;
import java.awt.*;
import java.math.*;
import java.text.DecimalFormat;
public class testMattWalker {
//
public static void main (String[] args){
Scanner input = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
Scanner input3 = new Scanner(System.in);
Scanner input4 = new Scanner(System.in);
Scanner input5 = new Scanner(System.in);
Scanner input6 = new Scanner(System.in);
Scanner input7 = new Scanner(System.in);
Scanner input8 = new Scanner(System.in);
int counter = 0;
int counter2 = 0;
int counterB = 0;
int counterB2 = 0;
int counterC = 0;
int counterD = 0;
int counterE = 0;
String yn = "";
String searchLast = "";
String searchFirst = "";
String searchNumber = "";
int maxNumberOfPeople = 5;
boolean go = true;
DecimalFormat phoneDecimalFmt = new DecimalFormat("0000000000");
java.text.MessageFormat phoneMsgFmt=new java.text.MessageFormat("({0})-{1}-{2}");
//Temp VAriables for entry
String firstNameOfEntry = "";
String lastNameOfEntry = "";
String personPhoneNumber = "";
//
//create array
String [][] array1 = new String[5][3];
while (go = true) {
String choice = "";
System.err.println("\n\n\n\n\n\n\n\n\nDIDGITAL PHONE BOOK 2013");
System.out.println("1- Create phone book\n2- Display phone book\n3- Find person(s) by last name\n4- Find person(s) by first name\n5- Find person(s) by phone number\n6- Exit application");
choice = input.nextLine();
if (choice.equals("1") && counter2 != maxNumberOfPeople) {
System.err.println("\n\n\n\n\nPHONE BOOK ENTRY CREATOR:");
System.out.println("Please enter the first name of the person you wish to enter: ");
array1[counter2][counter] = input2.nextLine();
counter++;
System.out.println("Please enter the last name of the person you wish to enter: ");
array1[counter2][counter] = input3.nextLine();
counter++;
System.out.println("Please enter the phone number of this person: example:9057773344");
array1[counter2][counter] = input4.nextLine();
counter = 0;
counter2++;
}else if (choice.equals("2")) {
int num = 0;
System.out.println("SEE I CAN FORMAT NUMBERS... I just didn't have time to put it on every one.");
for (int i = 0; i< array1.length; i++) {
num++;
System.err.println("Contact: " + num);
System.out.print(array1[counterB2][counterB]);
counterB++;
System.out.print(" " + array1[counterB2][counterB]);
counterB++;
String[] phoneNumArr= {
array1[counterB2][2].substring(0, 3),
array1[counterB2][2].substring(3,6),
array1[counterB2][2].substring(6)};
System.out.println(" ");
if (!array1[counterB2][2].equals(null)) {
System.out.println(phoneMsgFmt.format(phoneNumArr));
counterB = 0;
counterB2++;
}
}
}else if (choice.equals("3")) {
System.out.println("\n\n\n\n\n\nPlease enter the last name of the person you are searching for: ");
searchLast = input6.nextLine();
counterC = 0;
for (int i = 0; i < array1.length; i++) {
if (searchLast.equals(array1[counterC][1])) {
System.out.println(array1[counterC][0] + " " + array1[counterC][1] + " " + array1[counterC][2]);
}
counterC++;
}
}else if (choice.equals("4")) {
System.out.println("\n\n\n\n\n\nPlease enter the first name of the person you are searching for: ");
searchFirst = input7.nextLine();
counterD = 0;
for (int i = 0; i < array1.length; i++) {
if (searchFirst.equals(array1[counterD][0])) {
System.out.println(array1[counterC][0] + " " + array1[counterC][1] + " " + array1[counterC][2]);
}
counterD++;
}
}else if (choice.equals("5")) {
System.out.println("\n\n\n\n\n\nPlease enter the phone number of the person you are searching for: ");
searchNumber = input8.nextLine();
counterE = 0;
for (int i = 0; i < array1.length; i++) {
if (searchNumber.equals(array1[counterE][2])) {
System.out.println(array1[counterC][0] + " " + array1[counterC][1] + " " + array1[counterC][2]);
}
counterE++;
}
}else if (choice.equals("6")) {
System.err.println("Are you sure? [y/n]");
yn = input5.nextLine();
if (yn.equals("y")) {
System.err.println("CLOSING...");
System.exit(0);
}else if (yn.equals("n")){
System.out.println("Resuming...");
}else {System.err.println("ERROR"); System.exit(0);}
}
}
}// end of main
}// end of class
编辑:我一直在尝试创建一种方法,只显示电话号码,如果数组中的元素不为空但它似乎不想工作对我来说; - ;
答案 0 :(得分:2)
您收到NullPointerException
可能是因为您在检查{{1}的值之前引用了(String
)array1[counterB2][2]
的方法在这里:null
。
初始化if (!array1[counterB2][2].equals(null))
时会发生这种情况,phoneNumArr
包含substring
上array1[counterB2][2]
的来电。
如果array1[counterB2][2]
为null
,则在其上调用substring
会抛出NullPointerException
。
只需将substring
语句括在null
的支票中,您就可以了。
最后,不使用if (!array1[counterB2][2].equals(null))
,使用if (array1[counterB2][2] != null)
。
否则,您可能最终会在Object.equals
null
上调用Object
,这又会抛出NullPointerException
。
答案 1 :(得分:1)
如果用户确实通过
输入了某些内容,请检查nextLine() if(string != null)...
检查
答案 2 :(得分:1)
如果第一个对象为null,则使用'.equals(null)'比较字符串将抛出NullPointerException。相反,使用:
进行比较string == null
或显然
string != null
你想做什么。
您更喜欢.equals()方法进行字符串比较,但在检查某些内容是否为空时则不是。
我还注意到你的while循环的条件不会按预期工作,你每次使用'(go = true)'时都会给'go'布尔值赋值'true'。相反,要么使用:
while (go == true)
或只是
while (go)
答案 3 :(得分:0)
在使用Strings之前,你应该使用null检查。这是执行这些操作的标准方法。