Results 21 to 22 of 22
- 11-01-2008, 06:45 AM #21
Why is the "dictionary" array null in the getNextWord method?
Consider the code in your class constructor:
When you do call setUpDictionary before the getNextWord method the field/reference "dictionary" will still be null.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();
Why?
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 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];
The caller would likely want to save the return value in a variable:Java Code:public String getNextWord() { int n = rand.nextInt(dictionary.length); return dictionary[n]; }
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:String nextWord = getNextWord();
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: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]; }
You could do both if you wanted:Java Code:setNextWord();
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]; }
- 11-01-2008, 02:42 PM #22
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
Array of instances using Math.random()
By xgi1008 in forum New To JavaReplies: 16Last Post: 01-25-2011, 11:10 PM -
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
Using Random
By razmyasdfg in forum CLDC and MIDPReplies: 1Last Post: 07-27-2008, 10:47 PM -
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM -
generating random numbers in a 5x5 array.
By acidacid in forum New To JavaReplies: 3Last Post: 08-14-2007, 03:44 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks