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

Thread: getBet exception

  1. #1
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default getBet exception

    Exception in thread "main" java.lang.NullPointerException at NewFoothill.getBet() line: 35
    [code = java] String bet = scanner.nextLine();[/code]
    My eclipse debugger Expressions box states
    [code = java] x+y=? = (No explicit return value)[/code]
    and at NewFoothill.main(java.lang.String[]) line:17
     int bet = getBet();
    Here is the complete code in my main and my method for getBet().
    public static void main(String[] args) throws Exception
    {
       int bet = getBet();
       while (bet!=0)
       {
          String pullOne = pull();
          String pullTwo = pull();
          String pullThree = pull();
          int mult = getPayMultiplier(pullOne,pullTwo,pullThree);
          int winnings = bet*mult;
          display(pullOne,pullTwo,pullThree,winnings);
          bet=getBet();
       }
    }
     
    static int getBet()
    {
       System.out.println("Enter bet");
       Scanner scanner = null;
       String bet = scanner.nextLine();
       return Integer.parseInt(bet);
    }
    I do not under the debugger comment (No explicit return value) and why my method for getBet() does not work. Could someone point me in the right direction concerning the above problems. Thanks!


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: getBet exception

    Scanner scanner = null;
    ...
    sacanner.nextLine();

    how is null going to return anything?

    Try initiating the scanner to an instance of Scanner rather than null. Also hit up your favorite search engine for nextInt()

  3. The Following User Says Thank You to jps For This Useful Post:

    willie lee (September 13th, 2012)

  4. #3
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: getBet exception

    I got my program to compile. However, it does not compile correctly. The getPayMultiplier and static void display(int winnings) methods do not appear. Plus, when I first tried running my program after making the changes to the getBet() method my compiler gave me a warning about an obsolete method in <unknown declaring type>> and that it could damage the compiler. Could you please give me directions on how to paste my program onto this site so we can look at my program together in order to get some insight into how I can get it running correctly.

  5. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: getBet exception

    You can wait for an answer from someone with psychic powers, but this may take a long time, Another option is to show us the code that's not working as well as any error messages that you see (the actual messages, not a paraphrasing of the messages).

  6. The Following User Says Thank You to curmudgeon For This Useful Post:

    willie lee (September 17th, 2012)

  7. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: getBet exception

    Quote Originally Posted by willie lee View Post
    ... Could you please give me directions on how to paste my program onto this site so we can look at my program together in order to get some insight into how I can get it running correctly.
    Post everything just like you did in your first post. Ctrl+a to select all, Ctrl+v to copy, and once back in here, Ctrl+v to paste. Don't try to attach them as files. I (and many others) are just too lazy to download them to look at them.

    @curmudgeon -- : (

  8. The Following User Says Thank You to jps For This Useful Post:

    willie lee (September 17th, 2012)

  9. #6
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: getBet exception

    import java.util.Scanner;
    import java.util.*;
     
    public class NewFoothill
    {
       Scanner input = new Scanner(System.in);
       Random myRandom = new Random();
       int randomNumber;
       static int strBet = 0;
     
       public static void main (String [] args) throws Exception
       {
          while (getBet()!=0)
          {
     
             String pullOne = pull();
             String pullTwo = pull();
             String pullThree = pull();
             int mult = getPayMultiplier(pullOne,pullTwo,pullThree);
             int bet = 0;
             int winnings = bet*mult;
             display(pullOne,pullTwo,pullThree,winnings);
             bet=getBet();
          }
          System.out.println("Thanks for playing");
       }
     
       static int getBet()
       {
          System.out.println("Enter bet:  ");
          Scanner userInput;
          userInput = new Scanner(System.in);
          String strBet = userInput.nextLine();
          return Integer.parseInt(strBet);
       }
     
       static String pull()
       {
          double pull = Math.random();
          if (pull >= 0.25d)
          {
             return "BAR";
          }
          else if (pull >= 0.25d)
          {
             return "7";
          }
          else if (pull >= 0.25d);
          {
             return "CHERRIES";
          }
          else
          {
             return "SPACE";
          }
       }
     
       static int getPayMultiplier(String s1, String s2, String s3)
       {
          int cherryCnt = 0;
          if (s1.equals("CHERRIES"))
          {
             cherryCnt++;
          }
          if (s2.equals(CHERRIES"))
          {
             cherryCnt++;
          if (s3.equals("CHERRIES"))
          {
             cherryCnt++;
          }
          if (cherryCnt == 1)
          {
             return 3;
          }
          else if (cherryCnt == 2)
          {
             return 10;
          }
          else if (cherryCnt == 3)
          {
             return 20;
          else
          {
             if (s1.equals(s2) && s1.equals(s3))
             {
                if (s1.equals("BAR"))
                {
                   return 35;
                }
                else if (s2.equals("7"))
                {
                   return 50;
                }
             }
          }
          return 0;
       }
     
       static void display(String s1, String s2, String s3, int winnings)
       {
          System.out.println(s1 + " " + s2 + " " + s3);
     
          if (winnings == 0)
          {
             System.out.println("Sorry!");
          }
          else
          {
             System.out.println("You won $" + winnings);
          }
       }
    }
    [/code ]
     
    Output
    [code = java]
    Enter bet:
    100
    BAR BAR BAR
    Sorry!
    Enter bet:
    100
    Enter bet:
    100
    SPACE BAR BAR
    Sorry!
    Enter bet:
    1. The static String pull() method never returns "7" or "CHERRIES". It only returns "BAR" and "SPACE". I do not understand why. Also, after I enter one bet it goes through 50% of the loop, then when it asks for a bet and I enter it, it does nothing. I have to enter it again to get the loop working. See output code above.
    2. As you see, the method does not print the PayMultiplier or the winnings.
    3. The loop above does not continue correctly.
    Can you please give me some guidance so this program will work correctly. Thanks!
    Last edited by willie lee; September 20th, 2012 at 07:28 AM.

  10. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: getBet exception

       static String pull()
       {
          double pull = Math.random();
          if (pull >= 0.25d)
          {
             return "BAR";
          }
          else if (pull >= 0.25d)
          {
             return "7";
          }
          else if (pull >= 0.25d);
          {
             return "CHERRIES";
          }
          else
          {
             return "SPACE";
          }
       }
    When will this method ever return "7" or "CHERRIES"?

    Something looks fishy on this line:
    cherryCnt = ++;

  11. The Following User Says Thank You to jps For This Useful Post:

    willie lee (September 19th, 2012)

  12. #8
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: getBet exception

    The error was mine when I entered it on this website. The code in my program reads:
    if(s1.equals("CHERRIES"))
    {
       cherryCnt++;
    }
    Last edited by willie lee; September 19th, 2012 at 06:27 AM.

  13. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: getBet exception

    So do you still have a question? If so, what is it?
    The snippet you posted in #8 looks fine if it does what you intended for it to do

  14. The Following User Says Thank You to jps For This Useful Post:

    willie lee (September 20th, 2012)

  15. #10
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: getBet exception

    I still have many problems. Please look at my post on 17/09/2012, 0620 hours where I entered my entire program and the output from it. Plus, underneath the program mentioned above, I listed the problems I am having with it.
    Last edited by willie lee; September 20th, 2012 at 07:41 AM.

  16. #11
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: getBet exception

    Read my post #7. Have you fixed either of the two problems there? If so please post updated code and new questions. If not, see below:

    Do you see how the first if statement will block the following else if statements? This is because the conditions are all identical.
    any time pull >= 0.25d returns true, you get "BAR" and if it is false the next two conditions are also false so they are skipped leaving you with the final else, and you get "SPACE"

    if you don't understand what is wrong with the line:
    cherryCnt = ++;
    then search for use of ++ in java

  17. The Following User Says Thank You to jps For This Useful Post:

    willie lee (September 22nd, 2012)

  18. #12
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: getBet exception

    Thanks jps! Here is my static String pull() with new changes.
    static String pull()
    {
       double pull = Math.random();
       if (pull < 0.25d)
       {
          return "BAR";
       }
       else if (pull < 0.50)
       {
          return "7";
       else if (pull < 0.75d)
       {
          return "CHERRIES";
       }
       else
       {
          return "SPACE";
       }
    }
    output:
    Enter bet:
    100
    SPACE SPACE BAR
    Sorry!
    Enter bet:
    100
    Enter bet:
    100
    CHERRIES 7 BAR
    Problems on output:
    1. Great! "7" and "CHERRIES" are appearing.
    2. My loop is not allowing me to make a 2nd bet. I must enter a 3rd bet before the loop will function.
    3. My loop is not picking up the getPayMult() method. If "CHERRIES" is in the 1st position it should return a mult. of 3 and the winnings should be 300.

    Please give me further guidance. The program is working better because of your help. Thanks again.
    Last edited by willie lee; September 27th, 2012 at 12:18 AM.

  19. #13
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: getBet exception

    Could some one please look at my post on the 22nd of September.

  20. #14
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: getBet exception

    Quote Originally Posted by willie lee View Post
    Could some one please look at my post on the 22nd of September.
    I assume you mean in this thread.


    1. Great! "7" and "CHERRIES" are appearing.




    2. My loop is not allowing me to make a 2nd bet. I must enter a 3rd bet before the loop will function.
    3. My loop is not picking up the getPayMult() method. If "CHERRIES" is in the 1st position it should return a mult. of 3 and the winnings should be 300.
    If you have a new question ,depending on how related it is to the topic of the current thread, you may want to make a new thread, maybe not, but in either case you should post the code related to the question and any error messages.

  21. The Following User Says Thank You to jps For This Useful Post:

    willie lee (September 28th, 2012)

Similar Threads

  1. Replies: 2
    Last Post: August 30th, 2012, 10:45 AM
  2. Exception
    By prabhakar in forum Exceptions
    Replies: 5
    Last Post: July 21st, 2012, 07:51 AM
  3. Replies: 5
    Last Post: September 5th, 2011, 11:31 AM
  4. Replies: 6
    Last Post: March 25th, 2011, 04:42 PM
  5. can someone please help me im getting exception
    By kristynrod in forum Exceptions
    Replies: 2
    Last Post: March 15th, 2011, 05:40 PM