Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By jim829

Thread: Need help on "variable might not have been initialized"

  1. #1
    CrushingJava is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Need help on "variable might not have been initialized"

    For my assignment, I need to make a static method to create a random dice. Class One, Two, Three, Four, Five, Six are all subclasses of class Die.
    Here's my code:
    Java Code:
    public static Die newDie(int x, int y) {
           Die myDie;
           int randomNumber;
           randomNumber = Utilities.randomNumber(1,6);
           switch(randomNumber) {
              case 1: myDie = new One(x,y); 
                      break;
              case 2: myDie = new Two(x,y); 
                      break;
              case 3: myDie = new Three(x,y); 
                      break;
              case 4: myDie = new Four(x,y); 
                      break;
              case 5: myDie = new Five(x,y); 
                      break;
              case 6: myDie = new Six(x,y); 
                      break;
              }
              return myDie;
    }
    The problem is that, when I run this, the "myDie" after the "return" is outlined and an error message says "variable myDie might not have been initialized".
    I think the switch statement has already instantiated myDie, hasn't it? Thank you in advance.
    Last edited by CrushingJava; 06-27-2013 at 08:55 PM.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,931
    Rep Power
    4

    Default Re: Need help on "variable might not have been initialized"

    The compiler doesn't know that your random number utility will only generate a number between 1 and 6 inclusive. So it is telling you it has not been intialized. You could either provide a default case and assign some value to myDie there or you could just intialize the variable to null.

    Also, why are you using 6 different classes to generate a die? What is the difference between them?

    Regards,
    Jim
    CrushingJava likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    CrushingJava is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Re: Need help on "variable might not have been initialized"

    Quote Originally Posted by jim829 View Post
    The compiler doesn't know that your random number utility will only generate a number between 1 and 6 inclusive. So it is telling you it has not been intialized. You could either provide a default case and assign some value to myDie there or you could just intialize the variable to null.

    Also, why are you using 6 different classes to generate a die? What is the difference between them?

    Regards,
    Jim
    It works after I set myDie = null. Thank you so much!

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

    Default Re: Need help on "variable might not have been initialized"

    Having six subclasses of Die (one for each number) seems overkill to me. Imagine you are using a 20 sided or more die (like they use in D&D). Are you going to keep making classes ad infinitum?

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,931
    Rep Power
    4

    Default Re: Need help on "variable might not have been initialized"

    Yep. The OP didn't respond to my related question.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: Need help on "variable might not have been initialized"

    I also wonder why a Die needs co-ordinates.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need help on "variable might not have been initialized"

    Quote Originally Posted by Junky View Post
    I also wonder why a Die needs co-ordinates.
    Because then it knows where it is so it can find its way back to the dice stable; otherwise it would be a lost little die and we don't want that, do we?

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  3. Replies: 0
    Last Post: 04-20-2009, 11:08 AM
  4. [SOLVED] "start: applet not initialized"
    By DenniGa in forum Java Applets
    Replies: 3
    Last Post: 02-24-2009, 02:10 AM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •