Results 1 to 8 of 8
  1. #1
    stevenhaynes5 is offline Member
    Join Date
    May 2011
    Posts
    17
    Rep Power
    0

    Default java.lang.NullPointerException

    Hi,

    Im new to java and im just messing around with a few things. Ive put thew following code togther but i get a java.lang.NullPointerException.

    Java Code:
    package questionaireadvanced;
    import java.util.Scanner;
    
    public class QuestionaireAdvanced {
    
        public static void main(String[] args) {
            
            Scanner myScanner = new Scanner(System.in);
            
            String[] questions = new String[2];
            questions[0] = "Whats is my name";
            questions[1] = "What is my age";
            
            char[] answer = new char[2];
            
            for (int i = 0; i < questions.length; i++) {
                System.out.println(questions[i]);
                answer[i] = myScanner.findInLine(".").charAt(0);
                
                switch (answer[i]) {
                    case 'a':
                        System.out.println("Wrong!");
                        break;
                    case 'b':
                        System.out.println("Well done!");
                        break;
                    case 'c':
                        System.out.println("Wrong!");
                        break;
                    case 'd':
                        System.out.println("Wrong!");
                        break;
                    default:
                        System.out.println("You did not enter a, b, c, or d");
                        break;
                }
            }
            
            System.out.println("The program is now exiting");
        }
    }
    Any help would be very much appriciated.

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default

    Could we get a stacktrace of the exception (i.e. what is output to the console)? It makes it much easier pinpointing the error.

  3. #3
    stevenhaynes5 is offline Member
    Join Date
    May 2011
    Posts
    17
    Rep Power
    0

    Default

    Hi Toll,

    Here is the output

    Java Code:
    run:
    Whats is my name
    b
    Well done!
    What is my age
    Exception in thread "main" java.lang.NullPointerException
    	at questionaireadvanced.QuestionaireAdvanced.main(QuestionaireAdvanced.java:18)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 2 seconds)
    Thanks

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default

    It seems to indicate that the problem is in "answer[i] = myScanner.findInLine(".").charAt(0);". After reading the docs for Scanner (I must admit, I've never really used Scanner), I'm starting to believe that the problem stems from it not finding the pattern in the line. Does the program stop before you enter the second answer, or after?

  5. #5
    stevenhaynes5 is offline Member
    Join Date
    May 2011
    Posts
    17
    Rep Power
    0

    Default

    The second question is printed on the screen but ends when it want to take input. Here is another run.

    Java Code:
    run:
    Whats is my name
    a
    Wrong!
    Exception in thread "main" java.lang.NullPointerException
    What is my age
    	at questionaireadvanced.QuestionaireAdvanced.main(QuestionaireAdvanced.java:18)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 2 seconds)

  6. #6
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default

    Then I'm rather certain that the findInLine() method doesn't actually advance lines, but just looks in the last entered line. So it's trying to find the next character in the line with your first answer, and since it can't find any more characters, it returns null according to its specifications. Try finding another method to use; have a look at Scanner (Java Platform SE 6) to see if you can find one that suits you.

  7. #7
    stevenhaynes5 is offline Member
    Join Date
    May 2011
    Posts
    17
    Rep Power
    0

    Default

    After thinking about what you said i ran the program again with entering two charcters. The program ran without any errors.

    Java Code:
    run:
    Whats is my name
    ab
    Wrong!
    What is my age
    Well done!
    The program is now exiting
    BUILD SUCCESSFUL (total time: 2 seconds)
    I had a look through the scanner documentation you provided and i carnt see a methord that suits my needs in there.

    COuld you recommend somthing else?

  8. #8
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

Similar Threads

  1. java.lang.NullPointerException
    By nitinverma in forum CLDC and MIDP
    Replies: 4
    Last Post: 06-15-2010, 03:56 PM
  2. java.lang.nullPointerException
    By KSUliz in forum New To Java
    Replies: 10
    Last Post: 04-11-2010, 07:15 PM
  3. java.lang.NullPointerException
    By stevemcc in forum AWT / Swing
    Replies: 2
    Last Post: 02-08-2008, 09:01 AM
  4. java.lang.NullPointerException
    By ravian in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 07:39 PM
  5. java.lang.NullPointerException
    By Felissa in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 06:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •