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 9 of 9

Thread: Scanner Problem

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Scanner Problem

    Hello guys,

    I wrote a code from thomas wu book (OOP java, 5 edition).
    But I got an error in scanner statement like this
    debug:
    Your Age : 
    Exception in thread "main" java.lang.NullPointerException
    	at ProgrammerDefinedException.AgeInput5.getAge(AgeInput5.java:41)
    	at ProgrammerDefinedException.Ch8TestAgeInput5.main(Ch8TestAgeInput5.java:11)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 3 seconds)

    there's my code (I have given a mark where the error)
    /*
     * Buku Thomas Wu, Bab Programmer Defined Exception
     * Self Independent Declaration Modified Class
     */
    package ProgrammerDefinedException;
    import java.util.Scanner;
    import java.util.InputMismatchException;
     
    public class AgeInput5 {
        private static final String DEFAULT_MESSAGE = "Your Age : ";
        private static final int DEFAULT_TEPI_BAWAH = 0;
        private static final int DEFAULT_TEPI_ATAS = 99;
     
        private int tepiBawah;
        private int tepiAtas;
     
        private Scanner scanner;
     
        /***Constractor***/
        public AgeInput5(int low,int high)  {
            init(DEFAULT_TEPI_BAWAH,DEFAULT_TEPI_ATAS);
        }
     
        public AgeInput5(String msg,int low,int high) throws IllegalArgumentException{  //Throwing Exception
            if(low>high)
                throw new IllegalArgumentException("tepi bawah "+low+" lebih besar dari tepi atas "+high);
            else
                init(low,high);
            }
     
        /***Methode***/
        public int getAge() throws AgeInputException{
            return getAge(DEFAULT_MESSAGE);
        }
     
        public int getAge(String msg) throws AgeInputException{
            int umur;
            while(true){
              System.out.println(msg);
              try{
                  umur = scanner.nextInt(); <=========================================THIS IS AN ERRROR
                  if(umur<tepiBawah||umur>tepiAtas)
                      throw new AgeInputException(tepiBawah,tepiAtas,umur);
                  return umur;
              }
              catch(InputMismatchException e){
                  scanner.next();
                  System.out.println("Input is invalid \n" + "Please enter digit Only");
     
              }
            }
        }
     
        private void init(int low,int high){
            tepiBawah=low;
            tepiAtas=high;
        }
    }

    Are you guys know where's my mistake?
    thanks btw.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,623
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Scanner Problem

    Your Age :
    Exception in thread "main" java.lang.NullPointerException
    at ProgrammerDefinedException.AgeInput5.getAge(AgeInp ut5.java:41)
    There is a variable on line 41 that has a null value when the code is executed. Look at line 41, find the variable with the null value and backtrack in the code to see why that variable does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Scanner Problem

    Hi

    I know it's umur, so what should I do? assign the value of var umur before I scan it?

    Thanks

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,623
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Scanner Problem

    umur is an int variable. Primitive variables do not cause NPE. Only object variables can cause NPE when the code tries to use them to call a method for example.
    What statement is line 41? What object variable is on that line? Does it have a value?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Scanner Problem

    I see..
    this is a statement at line 41

    umur = scanner.nextInt()


    thanks

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,623
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Scanner Problem

    You didn't answer this part of my question:
    What object variable is on that line? Does it have a value?
    Hint: there is only one object variable on that line: scanner. Does it have a value?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Scanner Problem

    ohh,, scanner is an object on that line and scanner doesn't have any value before.
    so scanner have NULL value and umur take NULL value from it right?
    so how should I do for solve it, I mean I want umur have a value from user input.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,623
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Scanner Problem

    The variable: scanner needs to have the address of a Scanner object for the code to be able to call the Scanner class's nextIn() method. If scanner is null, you will het a NPE. Try this:
    String str = null;
    int val = str.length();   //  will give NPE because str is null
    scanner have NULL value and umur take NULL value from it
    Wrong for two reasons:
    1) there will be a NPE when trying to call the nextInt() method
    2) int variables can NOT have a null value.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Scanner Problem

    Thansk Norm I got it fixed.
    like you said we just need scanner have the addres, so we initialize .
    scanner= new Scanner(System.in);

Similar Threads

  1. Problem with Scanner
    By Rafal75 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 7th, 2012, 08:34 PM
  2. I am Facing problem in Scanner Class
    By snithishkumar in forum Java SE APIs
    Replies: 2
    Last Post: October 11th, 2011, 09:39 AM
  3. Scanner problem - need help
    By destructobob in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 23rd, 2011, 01:08 AM
  4. [SOLVED] Problem with Scanner ?
    By derekeverett in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 19th, 2010, 04:34 AM
  5. Simple scanner problem
    By Sinensis in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 20th, 2009, 09:01 PM