Results 1 to 20 of 20
Like Tree1Likes
  • 1 Post By PhQ

Thread: Null Pointer Exception

  1. #1
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default 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"};

  2. #2
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default 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?

  3. #3
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default 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)];

  4. #4
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default 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.

  5. #5
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Null Pointer Exception

    What would be a good "getter" method for it? Just like
    Java Code:
    public String getArray(){ return word; }
    or something?

  6. #6
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Null Pointer Exception

    That's fine. However, be careful with your return types.

  7. #7
    foulkelore is offline Member
    Join Date
    Dec 2012
    Location
    Des Moines, IA
    Posts
    35
    Rep Power
    0

    Default 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;
    }

  8. #8
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Null Pointer Exception

    You have most probably forgot to initialize the words array.

  9. #9
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default 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);
        }
    }

  10. #10
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Null Pointer Exception

    Can you provide the whole error?

  11. #11
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default 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)

  12. #12
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Null Pointer Exception

    You don't need the

    Java 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.

  13. #13
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Null Pointer Exception

    You get this error because your variable words is null. Like I said above, you need to initialize the variable.
    Wnt2bsleepin likes this.

  14. #14
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default 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.

  15. #15
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Null Pointer Exception

    Follow the code from the top to the bottom and see if you can find the error.

  16. #16
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Null Pointer Exception

    Quote Originally Posted by Army View Post
    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)

  17. #17
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default 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.

  18. #18
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Null Pointer Exception

    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.

  19. #19
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Null Pointer Exception

    Look at the order you are trying to access the array at

    Java Code:
    private String myGeneratedRandomWord = getWords();
    See anything wrong with that being there?

  20. #20
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Null Pointer Exception

    Quote Originally Posted by Wnt2bsleepin View Post
    Look at the order you are trying to access the array at

    Java Code:
    private String myGeneratedRandomWord = getWords();
    See anything wrong with that being there?
    Ah, ok, thanks. It works now XD

Similar Threads

  1. Null pointer Exception
    By aortell24 in forum New To Java
    Replies: 5
    Last Post: 07-20-2012, 11:38 PM
  2. null pointer exception
    By Herah in forum New To Java
    Replies: 1
    Last Post: 12-01-2011, 08:44 AM
  3. Help with Null Pointer Exception
    By Beginner in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 04:41 PM
  4. Help with null pointer exception
    By gammaman in forum New To Java
    Replies: 4
    Last Post: 07-14-2009, 12:23 AM
  5. Null Pointer Exception
    By ScKaSx in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 11:27 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
  •