Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    NullPointerException error

    I am working on a word jumble program
    I have char[] orginalWord declared publicly declared in jumble and orginalWord = new char[wordToUse.length()] inside public void generatePuzzle.

    I get the error when I am trying to use "orginalWord.length" inside the code below and other similar cases
    if(Random131.getRandomInteger(3) == 0){
    swap(Random131.getRandomInteger(originalWord.length),
    Random131.getRandomInteger(originalWord.length));

    I am not sure why orginalWord is a null value.

    Code:
    
    public class Jumble implements Puzzle {
    
    	char[] scrambledWord;
    	char[] originalWord;
    	String originalString;
    	String scrambledString;
    	int initialnumTransformations;
    	int usernumTransformations;
    
    	//generates unscrambled puzzle
    	public void generatePuzzle(java.lang.String wordToUse){
    Last edited by watamadman; 04-15-2008 at 04:30 PM.

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    because you never initialize originalWord and the only place you do you try to use its length

    Quote Originally Posted by watamadman View Post
    Code:
    public void generatePuzzle(java.lang.String wordToUse){
    	// originalWord = null;
    	originalWord = new char[originalWord.length];
    	int test = wordToUse.length();
    	for(int x = 0; x < wordToUse.length(); x++){
    		originalWord[x] = wordToUse.charAt(x);
    	}
    	System.out.println(test);
    }
    what you should be using instead is the wordToUse's length

    Code:
    originalWord = new char[wordToUse.length()];
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •