I just completed my homework(see it in attachment) but when I run it, it always appears like that
run: =================================== Menu Exception in thread "main" java.lang.NullPointerException =================================== A) String reversal B) Vowel capitalization C) <Tag> removal D) String complement E) Compute Boolean expression F) Quit =================================== at TextProcessor.getChoice(TextProcessor.java:75) at TextProcessor.main(TextProcessor.java:184) Choices:Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds)
my code is below
public class TextProcessor { // field declaration private Scanner keyboard; // constructor // print the program heading // set up the keyboard Scanner object public void constructor(){ System.out.print("Text Processor"); keyboard = new Scanner(System.in); } // show menu method: given, DO NOT MODIFY // print the menu lines public void showMenu(){ System.out.println("==================================="); System.out.println("Menu"); System.out.println("==================================="); System.out.println("A) String reversal"); System.out.println("B) Vowel capitalization"); System.out.println("C) <Tag> removal"); System.out.println("D) String complement"); System.out.println("E) Compute Boolean expression"); System.out.println("F) Quit"); System.out.println("==================================="); } // get choice from user // repeat until getting a valid choice // requirement: case-insensitive choice // i.e. accepts both UPPER and lower-CASE choice // assumption: takes the first character of the input line // return a char, indicating the user's choice public char getChoice(){ //TO DO String str = "initial"; char result = 'z'; while (str.charAt(0) != 'a'&&str.charAt(0) != 'A'&& str.charAt(0) != 'b'&&str.charAt(0) != 'B'&& str.charAt(0) != 'c'&&str.charAt(0) != 'C'&& str.charAt(0) != 'd'&&str.charAt(0) != 'D'&& str.charAt(0) != 'e'&&str.charAt(0) != 'E'&& str.charAt(0) != 'f'&&str.charAt(0) != 'F'){ System.out.print("Choices:"); str = keyboard.next(); if (str.charAt(0) == 'a' || str.charAt(0) == 'A') result = 'A'; if (str.charAt(0) == 'b' || str.charAt(0) == 'B') result = 'B'; if (str.charAt(0) == 'c' || str.charAt(0) == 'C') result = 'C'; if (str.charAt(0) == 'd' || str.charAt(0) == 'D') result = 'D'; if (str.charAt(0) == 'e' || str.charAt(0) == 'E') result = 'E'; if (str.charAt(0) == 'f' || str.charAt(0) == 'F') result = 'F'; } return result; } // string reversal method // ... public void stringReversal(){ //TO DO System.out.print("Input a line:"); String input = keyboard.nextLine(); System.out.print("Reverse-line:"); int length = input.length(); for(int i = length - 1; i >= 0; i--){ System.out.print(input.charAt(i)); } } // vowel capitalization public void vowelCapitalization(){ //TO DO System.out.print("Input a line:"); String input = keyboard.nextLine(); input = input.replace('a', 'A'); input = input.replace('e', 'E'); input = input.replace('i', 'I'); input = input.replace('o', 'O'); input = input.replace('u', 'U'); System.out.print("VOWELED-line:"); System.out.print(input); } // <tag> removal // assumption: arrowed <> tags must appear in pair // assumption: no nested <> tags public void tagRemoval(){ //TO DO System.out.print("Input a line:"); String input = keyboard.nextLine(); int length = input.length(); for (int i = 0; i < length; i++ ){ if (input.charAt(i) != '<') System.out.print(input.charAt(i)); else { i++; while (input.charAt(i) != '>') i++; } } } //string complement public void stringComplement(){ //To do System.out.print("Input a line:"); String input = keyboard.nextLine(); int length = input.length(); for (int i = 0; i < length; i++){ if ((input.charAt(i) > 'A' && input.charAt(i) <= 'Z') ||(input.charAt(i) > 'a' && input.charAt(i) <= 'z')) System.out.print(input.charAt(i-1)); else { if (input.charAt(i) == 'A' || input.charAt(i) == 'a') System.out.print(input.charAt(i+25)); else { System.out.print(input.charAt(i)); } } } } // main program public static void main(String[] args) { // create the TextProcessor object TextProcessor processorObject = new TextProcessor(); while (true) { // ask the processorObject to show menu processorObject.showMenu(); // use the processorObject to ask for user choice char userChoice = processorObject.getChoice(); // process the user choice and send relevant messages if(userChoice == 'A'){ processorObject.stringReversal(); //uncomment this line if you implemented this function continue; } else if(userChoice == 'B'){ processorObject.vowelCapitalization(); //uncomment this line if you implemented this function continue; } else if(userChoice == 'C'){ processorObject.tagRemoval(); //uncomment this line if you implemented this function continue; } else if(userChoice == 'D'){ processorObject.stringComplement(); //uncomment this line if you implemented this function continue; } else if(userChoice == 'E'){ processorObject.expressionEvaluation(); //uncomment this line if you implemented the bonus part continue; } else break; // end the menu loop } System.out.println("Bye bye!"); }
I would appreciate it if somebody can help~