Re: Null Pointer Exception
I don't see any highlighting, but it may be my eyes. I am assuming that the error is on line 3 in your first segment of code? Did you define the randy variable? Do you have access to the words array?
Re: Null Pointer Exception
What do you mean, access to the array? That might be my problem. How do you access an array? The bit that is highlighted is Code:
String randomWord = words[randy.nextInt(words.length)];
Re: Null Pointer Exception
It is declared private. If it's not in the same class you won't be able to access it without a getter method. I ran the code you have above in a simple test class and it worked fine. It has to be something with your class organization.
Re: Null Pointer Exception
What would be a good "getter" method for it? Just like Code:
public String getArray(){ return word; }
or something?
Re: Null Pointer Exception
That's fine. However, be careful with your return types.
Re: Null Pointer Exception
Usually the getters and setter are the same name as the variables. Like this:
Code:
public String[] getWords() {
return words;
}
public void setWords(String[] words) {
this.words = words;
}
Re: Null Pointer Exception
You have most probably forgot to initialize the words array.
Re: Null Pointer Exception
Still throws a NullPointerException.
Code:
/**
* TODO:
* Make it print dashes for the letters.
* Make it print the letter where index is.
* Make it continue after start.
* Add letter to the continue method.
* Compare random word and selected indexes.
* Find index of random word, use that index to put the letter into that index
* then replace the '_' with the letter with that index.
* Index with Element of the word
* ...
*/
/** Import necessary packages*/
import java.util.Random;
import javax.swing.JOptionPane;
import java.util.Scanner;
import java.lang.String;
public class Hangman extends BasicGame
{
/**Call all of the variables*/
Random randy = new Random();
private int guesses;
private final int maxGuesses = 6;
private String myGeneratedRandomWord = getWords();
private String letter;
private String randomWord = "";
private String itsGuessedLetter = "";
private String itsSolvedWord = "-----";
private String[] words = {"apple", "beret", "arose", "along", "beamy", "becks", "decks", "barks",
"stark", "start", "stabs", "baggy", "asked", "asset", "asses", "audit",
"bowls", "boxes", "seats", "balls", "boats", "boxer", "brick", "bound",
"brass", "caked", "braid", "caged", "essay", "fault", "dents", "dutch",
"ethos", "dunks", "pains", "faxes", "mummy", "mixer", "mills", "might",
"moral", "teeth", "wings", "works", "walls", "tolls", "crawl", "toxin",
"bangs", "tough"};
public String[] getWord(){
return words;
}
/**Get the random word from the array*/
public String getWords (){
String randomWord = getWord()[randy.nextInt(getWord().length)];
return randomWord;
}
/**Get the indexes of the letter of the random word indices don't start with 1*/
public String getSelected(){
return itsGuessedLetter;
}
/**Finds index of the letter of randomWord*/
public int getIndex(){
int index = getWords().indexOf(getSelected());
return index;
}
/**Get the first input, set strikes to zero, get the random word*/
public void start(){
guesses = 0;
getWords();
JOptionPane.showMessageDialog(null, "Your word is: " +itsSolvedWord);
itsGuessedLetter = JOptionPane.showInputDialog(null, "Make your guess!");
int index = myGeneratedRandomWord.indexOf(itsGuessedLetter);
if(myGeneratedRandomWord.indexOf(itsGuessedLetter ) != -1) {
// it contains the letter
if(index >= 0){
itsSolvedWord = itsSolvedWord.substring(0,index)+itsGuessedLetter+itsSolvedWord.substring(index + 1);
JOptionPane.showMessageDialog(null, "Updated word: " +itsSolvedWord);
}
}
else {
// wrong guess
guesses++;
}
}
/**Continue the game on, checks if the letter is there if not then apply the letter*/
public void continueGame(){
itsGuessedLetter = JOptionPane.showInputDialog(null, "Make another guess!");
int index = myGeneratedRandomWord.indexOf(itsGuessedLetter);
if(myGeneratedRandomWord.indexOf(itsGuessedLetter ) != -1) {
// it contains the letter
itsSolvedWord = itsSolvedWord.substring(0,index)+itsGuessedLetter+itsSolvedWord.substring(index + 1);
}
else {
// wrong guess
guesses++;
}
}
/** Runner basically*/
public void mainScreen (){
start();
while (guesses != maxGuesses){
continueGame();
checkBodyParts();
}
gameOver();
}
/**Gets the */
/**After certain strikes head is called etc.*/
public void checkBodyParts(){
if(guesses == 1){
JOptionPane.showMessageDialog(null, "You now have a head! Wrong guesses left: 5");
}
if(guesses == 2){
JOptionPane.showMessageDialog(null, "You now have a body! Wrong guesses left: 4");
}
if(guesses == 3){
JOptionPane.showMessageDialog(null, "You now have a right arm! Wrong guesses left: 3");
}
if(guesses == 4){
JOptionPane.showMessageDialog(null, "You now have a left arm! Wrong guesses left: 2");
}
if(guesses == 5){
JOptionPane.showMessageDialog(null, "You now have a right leg! Wrong guesses left: 1");
}
if(guesses == maxGuesses){
JOptionPane.showMessageDialog(null, "You lose!");
}
}
/**You lose.*/
public void gameOver(){
JOptionPane.showMessageDialog(null, "Game Over!");
System.exit(0);
}
}
Re: Null Pointer Exception
Can you provide the whole error?
Re: Null Pointer Exception
Code:
java.lang.NullPointerException
at Hangman.getWords(Hangman.java:46)
at Hangman.<init>(Hangman.java:28)
at GameApp.main(GameApp.java:9)
Re: Null Pointer Exception
You don't need the
Code:
public String[] getWord(){
return words;
}
because the words array is visible to the class. You don't need a getter. You need to debug a bit. Try separating line 46 into different segments.
Re: Null Pointer Exception
You get this error because your variable words is null. Like I said above, you need to initialize the variable.
Re: Null Pointer Exception
How do I initialize it??? Everything I go through it says it's initialized.
Re: Null Pointer Exception
Follow the code from the top to the bottom and see if you can find the error.
Re: Null Pointer Exception
Quote:
Originally Posted by
Army
How do I initialize it??? Everything I go through it says it's initialized.
Read through this: Lesson: Language Basics (The Java™ Tutorials > Learning the Java Language)
Re: Null Pointer Exception
Really, if you know where it is just tell me. I've had this problem for two weeks now and I'm pretty tiered of it.
Re: Null Pointer Exception
Quote:
Originally Posted by
PhQ
Alternatively, you can use the shortcut syntax to create and initialize an array:
int[] anArray = {
100, 200, 300,
400, 500, 600,
700, 800, 900, 1000
};
Tells me nothing.
Re: Null Pointer Exception
Look at the order you are trying to access the array at
Code:
private String myGeneratedRandomWord = getWords();
See anything wrong with that being there?
Re: Null Pointer Exception
Quote:
Originally Posted by
Wnt2bsleepin
Look at the order you are trying to access the array at
Code:
private String myGeneratedRandomWord = getWords();
See anything wrong with that being there?
Ah, ok, thanks. It works now XD