gpt4 book ai didi

java - 字符串格式和 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 18:39:57 24 4
gpt4 key购买 nike

每当我为我的电话号码之一赋予值(空)并使用当前代码块将其打印到屏幕时,它就会给我一个 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

编辑:我一直在尝试做的是创建一种方法,仅在数组中的元素不为空时才显示电话号码,但它似乎不想工作对我来说;--;

最佳答案

您将获得 NullPointerException可能是因为您引用了 ( 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.equalsnullObject ,这又会抛出 NullPointerException .

关于java - 字符串格式和 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20354246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com