Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Word Guess program with a Null Pointer Exception

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Word Guess program with a Null Pointer Exception

    Hello,

    I keep receiving the Null Pointer Exception error in my code and I am not sure how to fix it. The program I am working on is a Word Guess program that is kind of like jeopardy. I am coding this in Eclipse, by the way.

    public class WordGuess 
    {
    	//global variables
    	static Scanner myScanner;
    	static Scanner datScanner;
    	static File inFile;
    	static String[] words = new String[10];
    	static String guessWord;
    	static String dashWord;
    	static String numCtr;
    	static String guess;
     
         public static void input()
    	{
    		int i = 0;
    		int guessCtr = 15;
    		dashWord = guessWord;
    		dashWord.replaceAll("[A-Z]", "-"); //this is where I get the error
     
    		System.out.println("Guesses left: " + guessCtr + "    " + dashWord);
     
    		System.out.print("Enter letter: ");
    		guess = myScanner.next();
     
    		guessValid();
     
     
    			for(i = 0; i < guessWord.length() && guessCtr > 0; i++)
    			{
    				if(guessValid())
    				{
    					if(guessWord.substring(i, i + 1).equals(guess))
    					{
    						guess.replace(dashWord.substring(i, i + 1), guess);
    					}
    					else
    					{
    						guessCtr--;
    					}
     
    					System.out.println("Guesses left: " + guessCtr + "    " + guessWord);
     
    					System.out.print("Enter letter: ");
    					guess = myScanner.next();
     
    				}
    			}
     
    			System.out.println("Sorry, you fail!");
    	}


  2. #2
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: Word Guess program with a Null Pointer Exception

    Usually when your compiler receives a NullPointerException, it is when you are trying to use a object that points to a null value.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Word Guess program with a Null Pointer Exception

    Yes, I know that it points to a null value, but I cannot figure out why dashWord would have a null value.

  4. #4
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Word Guess program with a Null Pointer Exception

    your guessWord is get a null value..Because you only declare the guessword like this static String guessWord; after that you didn't assign values on that.. and also directly you pass the guessword into dashword like this
    dashWord = guessWord; with out assigning

    i think this is the reason the null pointer exception is come on runtime...

    So you try to assign some value to guessword after that you pass the values into another variable like this..
    guessWord="Hi";
    dashWord = guessWord;
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

Similar Threads

  1. Null pointer exception in inventory program
    By tunrida in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 14th, 2012, 12:35 PM
  2. Help With Null Pointer Exception
    By kendraheartt in forum What's Wrong With My Code?
    Replies: 17
    Last Post: August 15th, 2012, 10:41 PM
  3. Null Pointer Exception
    By kendraheartt in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 15th, 2012, 10:33 PM
  4. [SOLVED] Null Pointer Exception
    By wltrallen2 in forum Object Oriented Programming
    Replies: 7
    Last Post: May 27th, 2012, 10:21 AM
  5. Null Pointer Exception?
    By SeanEE89 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 16th, 2011, 06:21 PM

Tags for this Thread