Page 2 of 2 FirstFirst 12
Results 21 to 22 of 22
  1. #21
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Why is the "dictionary" array null in the getNextWord method?
    Consider the code in your class constructor:
    Java Code:
        public HangmanPanel2() {
            buildGUI();
            setVisible(true);
            // Here you are calling [i]getNextWord[/i] before
            // you call [i]setUpDictionary[/i]. So the
            // reference/variable "dictionary" will still be null.
            secretWord = getNextWord();
            setUpStars();
    When you do call setUpDictionary before the getNextWord method the field/reference "dictionary" will still be null.
    Why?
    Java Code:
    public class HangmanPanel2 extends
        ...
        // Member variable declaration in class scope.
        public String dictionary[];
        ...
        public void setUpDictionary() {
            // Local variable declared in this methods scope,
            // ie, within curley braces (body) of this method.
            // This declaration "shadows"/"hides" the member
            // variable declared in class scope. The member
            // variable remains null, ie, it has not been
            // initialized/instantiated. To fix this,
            // comment-out the local variable declaration:
    //        String dictionary[];
            // Now this line will instantiate the member variable.
            dictionary = new String[20];
    About the getNextWord method. I may have contributed to the confusion here. If the purpose of the method is to get an element from the "dictionary" array and send it back to the caller, ie, return it, then you would return the element at the random index "n":
    Java Code:
        public String getNextWord() {
            int n = rand.nextInt(dictionary.length);
            return dictionary[n];
        }
    The caller would likely want to save the return value in a variable:
    Java Code:
    String nextWord = getNextWord();
    If the purpose of the method is to set a value for the "secretWord" field then you do not have to return a value:
    Java Code:
        public void setNextWord() {
            // Pick a random index in the array.
            int n = rand.nextInt(dictionary.length);
            // Assign the word at this random index to
            // the "secretWord" field.
            secretWord = dictionary[n];
        }
    The caller of this method will not expect a return value because the method signature specifies a "void" return type, ie, it doesn't return anything to the caller. The caller would simply do:
    Java Code:
    setNextWord();
    You could do both if you wanted:
    Java Code:
        public String getNextWord() {
            int n = rand.nextInt(dictionary.length);
            // Assign value to member_variable/field.
            secretWord = dictionary[n];
            // Return value to caller.
            return dictionary[n];
        }

  2. #22
    Franneldort is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    Thanks so much for helping! It's working now :) When I run it, it selects a random word from the dictionary array and sets it to the JTextField that I wanted.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Array of instances using Math.random()
    By xgi1008 in forum New To Java
    Replies: 16
    Last Post: 01-25-2011, 11:10 PM
  2. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  3. Using Random
    By razmyasdfg in forum CLDC and MIDP
    Replies: 1
    Last Post: 07-27-2008, 10:47 PM
  4. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM
  5. generating random numbers in a 5x5 array.
    By acidacid in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 03:44 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
  •