HW异常(NullPointerException)

时间:2014-11-19 02:58:08

标签: java exception

import java.util.Scanner;
import java.util.InputMismatchException;

public class TestMorseCode {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Morse Code Conversion Program.");
    System.out
            .println("This program reads a phrase in English(or Morse code) and prints its equivalent in Morse code (or English).");
    for (;;) {
        int choice = 0;
        System.out.println();
        System.out
                .println("Please select one [1-3]: \n1: English -> Morse \n2: Morse -> English \n3: Quit");
        try {
            choice = input.nextInt();
        } catch (InputMismatchException e) {
            System.out.println("Invalid input");
            System.out.println("-------- EXCEPTION --------");
            e.printStackTrace();
            System.out.println("---------------------------");
            input.nextLine();
            continue;
        }
        input.nextLine();
        if (choice == 1) {
            System.out.println("Enter an English phrase:");
            String phrase = input.nextLine();
            int j = phrase.length();
            for (int i = 0; i < j; i++) {
                try {
                    System.out
                            .print(MorseCode.toMorseCode(phrase.charAt(i)));
                } catch (AlphabetException e) {
                    System.out.println(e);
                    e.printStackTrace();
                    continue;
                }
                System.out.print(" ");
            }
        } else if (choice == 2) {
            System.out.println("Enter a Morse phrase:");
            String phrase = input.nextLine();
            String[] words = phrase.split("\\s+");
            int w = words.length;
            System.out.println(w);
            char[][] code = new char[w][];
            for (int i = 0; i < w; i++) {
                for (int j = 0; j < words[i].length(); j++) {
                    code[i][j] = words[i].charAt(j);
                }

                for (int k = 0; i < code[k].length; k++) {
                    try {
                        System.out.print(MorseCode.fromMorseCode(code[k]));
                    } catch (AlphabetException e) {
                        System.out.println(e);
                        e.printStackTrace();
                        continue;
                    }
                    System.out.print(" ");
                }
            }
        } else if (choice == 3) {
            System.exit(0);
        } else {
            continue;
        }
        System.out.println();
    }
}
}

我遇到了问题,我一直收到错误

的toMorseCode方法
Exception in thread "main" java.lang.NullPointerException
    at TestMorseCode.main(TestMorseCode.java:50)

第50行对应于:

 code[i][j] = words[i].charAt(j);

1 个答案:

答案 0 :(得分:0)

您永远不会初始化代码数组的第二维,因此它保持为null,因为在开始填充之前必须初始化所有数组。那样做:

char[][] code = new char[w][];
for (int i = 0; i < w; i++) {

    //   **** here ****
    code[i][] = new char[words[i].length()]; // *****

    for (int j = 0; j < words[i].length(); j++) {
        code[i][j] = words[i].charAt(j);
    }