Hallo,
ich bekomme zu folgendem Code ein NullPointerException:
Der Fehler selber sagt mir, dass die Exception im ersten If statement in der decode Methode geworfen wird. Allerdings kann ich mittels der Debug Funktion nicht herausfinden, wie ich das Problem fixen kann. Hat jemand einen Vorschlag, woran es genau liegen kann, dass die Exeption geworfen wird?
ich bekomme zu folgendem Code ein NullPointerException:
Java:
package programming.set9.morses;
public class MorseCoder {
final static String[] morseArray = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-",
".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." };
/**
* Translates alphabet letter to code letter.
*
* @param c
* @return String
*/
private static String alphabetToCodeLetter(char c) {
if (c < 'A' || c > 'Z') {
return "";
} else {
return morseArray[Character.getNumericValue(c) - Character.getNumericValue('A')];
}
}
/**
* Translates code letter to alphabet letter.
*
* @param codeLetter
* @return Character or null.
*/
private static Character codeLetterToAlphabet(String codeLetter) {
for (int i = 0; i < morseArray.length; i++) {
if (codeLetter.equals(morseArray[i]))
return (char) ((int) ('A') + i);
}
return null; // if codeLetter was not a code string
}
/**
* Returns a new string which is the natural-language version of the input
* string, which is assumed to be in Morse code format as produced by the encode
* method.
*
* @param input
* Morse code input string.
* @return natural language version or {@code null} if the input string could
* not be properly parsed.
*/
public static String decode(String input) {
StringBuffer code = new StringBuffer();
String[] words = input.split("\n"); // split to words
for (int i = 0; i < words.length; i++) {
String[] letters = words[i].split(" ");
for (int j = 0; j < letters.length; j++) {
if (letters[i] != morseArray[j]) {
return null;
}
Character c = codeLetterToAlphabet(letters[j]);
if (c == null) { // ignore invalid input
continue;
}
code.append(c);
}
if (i < words.length - 1) // add space if sentence is not finished
code.append(' ');
}
return code.toString();
}
/**
* Returns a new string which is the Morse code version of the input string.
* Each word is on a separate line. The morse representation of each character
* of the input string is separated from the next by one or more space
* characters in the output string.
*
* @param input
* the input string.
* @return the Morse code version of the input string, ignoring all characters
* in the input string that cannot be represented in international morse
* code.
*/
public static String encode(String input) {
input = input.toUpperCase();
StringBuffer code = new StringBuffer();
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
code.append(c == ' ' ? '\n' : alphabetToCodeLetter(c) + ' ');
}
return code.toString();
}
public static void main(String[] args) {
System.out.println(MorseCoder.decode(".-.-.-.-"));
}
}
Der Fehler selber sagt mir, dass die Exception im ersten If statement in der decode Methode geworfen wird. Allerdings kann ich mittels der Debug Funktion nicht herausfinden, wie ich das Problem fixen kann. Hat jemand einen Vorschlag, woran es genau liegen kann, dass die Exeption geworfen wird?