...

View Full Version : NullPointerException error



watamadman
04-14-2008, 03:08 AM
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.





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

shyam
04-14-2008, 07:09 AM
because you never initialize originalWord and the only place you do you try to use its length



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


originalWord = new char[wordToUse.length()];



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum