Results 1 to 20 of 20
Thread: Null Pointer Exception
- 01-18-2013, 05:56 PM #1
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Null Pointer Exception
I keep getting a Null Pointer Exception when I run my code. This is the bit that is highlighted.
Java Code:/**Get the random word from the array*/ public String getWords (){ String randomWord = words[randy.nextInt(words.length)]; return randomWord; }Java Code: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"};
- 01-18-2013, 06:43 PM #2
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
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?
- 01-18-2013, 08:09 PM #3
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
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
Java Code:String randomWord = words[randy.nextInt(words.length)];
- 01-18-2013, 08:10 PM #4
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
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.
- 01-18-2013, 08:20 PM #5
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Re: Null Pointer Exception
What would be a good "getter" method for it? Just like
or something?Java Code:public String getArray(){ return word; }
- 01-18-2013, 09:25 PM #6
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Null Pointer Exception
That's fine. However, be careful with your return types.
- 01-18-2013, 09:28 PM #7
Member
- Join Date
- Dec 2012
- Location
- Des Moines, IA
- Posts
- 33
- Rep Power
- 0
Re: Null Pointer Exception
Usually the getters and setter are the same name as the variables. Like this:
Java Code:public String[] getWords() { return words; } public void setWords(String[] words) { this.words = words; }
- 01-18-2013, 11:08 PM #8
Re: Null Pointer Exception
You have most probably forgot to initialize the words array.
- 01-18-2013, 11:50 PM #9
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Re: Null Pointer Exception
Still throws a NullPointerException.
Java 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); } }
- 01-19-2013, 12:37 AM #10
Re: Null Pointer Exception
Can you provide the whole error?
- 01-19-2013, 01:12 AM #11
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Re: Null Pointer Exception
Java Code:java.lang.NullPointerException at Hangman.getWords(Hangman.java:46) at Hangman.<init>(Hangman.java:28) at GameApp.main(GameApp.java:9)
- 01-19-2013, 01:17 AM #12
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Null Pointer Exception
You don't need the
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.Java Code:public String[] getWord(){ return words; }
- 01-19-2013, 01:26 AM #13
Re: Null Pointer Exception
You get this error because your variable words is null. Like I said above, you need to initialize the variable.
- 01-19-2013, 01:31 AM #14
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Re: Null Pointer Exception
How do I initialize it??? Everything I go through it says it's initialized.
Last edited by Army; 01-19-2013 at 01:37 AM.
- 01-19-2013, 01:39 AM #15
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Null Pointer Exception
Follow the code from the top to the bottom and see if you can find the error.
- 01-19-2013, 01:42 AM #16
Re: Null Pointer Exception
Read through this: Lesson: Language Basics (The Java™ Tutorials > Learning the Java Language)
- 01-19-2013, 01:42 AM #17
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
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.
- 01-19-2013, 01:47 AM #18
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
- 01-19-2013, 01:50 AM #19
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Null Pointer Exception
Look at the order you are trying to access the array at
See anything wrong with that being there?Java Code:private String myGeneratedRandomWord = getWords();
- 01-19-2013, 04:14 AM #20
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Similar Threads
-
Null pointer Exception
By aortell24 in forum New To JavaReplies: 5Last Post: 07-20-2012, 11:38 PM -
null pointer exception
By Herah in forum New To JavaReplies: 1Last Post: 12-01-2011, 08:44 AM -
Help with Null Pointer Exception
By Beginner in forum New To JavaReplies: 2Last Post: 04-17-2010, 04:41 PM -
Help with null pointer exception
By gammaman in forum New To JavaReplies: 4Last Post: 07-14-2009, 12:23 AM -
Null Pointer Exception
By ScKaSx in forum New To JavaReplies: 1Last Post: 01-24-2009, 11:27 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks