Results 1 to 9 of 9
  1. #1
    cfox10 is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default About to pull my hair out. :(

    Hey guys, I'm in CS 162 at my college, the class is already destroying my brain and it's only the 2nd week. I finished CS 161 with an A and a general knowledge of java but I feel that I've already forgotten everything. Anyway, I have a problem and I was hoping someone could help me.

    I have the following code written so far, it's supposed to be a guessing game between 0 and 100. This is the first (and easy) part of the lab and I'm already wanting to give up..

    import java.util.Random;
    /**
    * This is a guessing game that allows you to input a number
    * between 0 and 100, you're trying to guess the random number
    * the computer generates.
    *
    * @author Christopher Fox
    * @version 1.0
    */
    public class GuessANumber
    {
    // instance variables - replace the example below with your own
    private int x;
    private int nextInt;

    /**
    * Constructor for objects of class GuessANumber
    */
    Random randomGenerator;
    Random generator = new Random();
    int index = randomGenerator.nextInt(101);
    int guesses = randomGenerator.nextInt();
    {
    // initialise instance variables
    int guesses = 0;
    }

    /**
    * An example of a method - replace this comment with your own
    *
    * @param y a sample parameter for a method
    * @return the sum of x and y
    */
    public void nextInt()
    {
    // put your code here
    while (guesses == 0){
    System.out.println("Please make a guess " );
    guesses++;
    }
    if(guesses == index)
    System.out.println("You are correct ");
    else if (guesses > index)
    System.out.println("Your guess is too high ");
    else
    System.out.println("Your guess is too low ");

    while(guesses != index){
    System.out.println("Please try again " );
    }
    }
    }

    The area I have put in bold keeps giving me a null pointer exception.. I'm not sure why.. I have another class that has the other code I need (pre-written) for the lab.
    Any help would be appreciated, I've spent forever on this and feel it's a lost cause. Time to change majors.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Java Code:
    Random randomGenerator;
    When do you create the Random object? Hint: why have you got two Random variables?

  3. #3
    cfox10 is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Java Code:
    Random randomGenerator;
    When do you create the Random object? Hint: why have you got two Random variables?
    Random randomGenerator;
    Random generator = new Random();

    is that not correct for creating it?
    Also, I'm not sure.. I don't understand where I put the code to set use number of guesses to 0.

    Would it just be guesses == 0;
    and where would I place it?
    I don't know where I initialize it I guess is what I'm trying to get at.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    No you have declared 2 variables of type Random (randomGenerator and generator). Only one refers to a Random object that has been created. Your code is similar to:
    Java Code:
    String x;
    String y = "Fred";
    System.out.println(x);
    and you wonder why it doesn't print "Fred"

  5. #5
    cfox10 is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    So, it should be:

    Random randomGenerator;
    int index = randomGenerator.nextInt(101);
    {
    // initialise instance variables
    guesses = 0;
    }
    Correct?

    Even then, I get a null pointer exception. Changing where I declare a new randomGenerator doesn't seem to make a difference?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    No. randomGenerator is still null. You deleted the wrong line of code.

  7. #7
    cfox10 is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    It seems as though no matter which lines of code I remove I get a null pointer..
    Not sure where to go from here

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    I hate to be rude but how on earth did you get an A?
    Java Code:
    Random randomGenerator = new Random();
    It really isn't that difficult.

  9. #9
    cfox10 is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    I honestly don't take any offense to it. I honestly don't know how I got an A in both.

    I'm just forgetting everything over time I guess.

Similar Threads

  1. Junit. Pulling my HAIR OUT!
    By phil128 in forum Eclipse
    Replies: 2
    Last Post: 10-04-2010, 08:06 PM
  2. Replies: 0
    Last Post: 11-15-2008, 08:29 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
  •