Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: why could this happen?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question why could this happen?

    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~
    Attached Files Attached Files


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,327
    Thanks
    181
    Thanked 828 Times in 771 Posts
    Blog Entries
    5

    Default Re: why could this happen?

    That exception is quite descriptive: go to the line that is noted (line 75, which is in the getChoice() method). Use println's to see what could be null (hint: when is keyboard ever instantiated? - where you think it is, add some println's to verify this is the case - you will hopefully see it is not). Another hint: see the following: Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Re: why could this happen?

    Quote Originally Posted by copeg View Post
    That exception is quite descriptive: go to the line that is noted (line 75, which is in the getChoice() method). Use println's to see what could be null (hint: when is keyboard ever instantiated? - where you think it is, add some println's to verify this is the case - you will hopefully see it is not). Another hint: see the following: Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Thank u so much! I have found what I did wrong.