每当我不给我的电话号码之一(空)赋值并使用此当前代码块将其打印到屏幕上时,它就会给我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.equals null上调用Object,这将再次抛出NullPointerException

10-01 19:03