I've read some topics here about the NullPointerException and i understand the point, but i just don't know how to fix this problem with my code. Here's the code:
I have created this array in another method ... but why it is not working in hashArray method? What should i do with array words .. to get it work correctly? [I inserted line breaks into one really long line - Jim] [ January 10, 2004: Message edited by: Jim Yingst ]
<edit> I apparently was typing this up at the same time Vicken was posting his comments, so it echos some of his comments.</edit> The problem is with your toArray() method: In this code block:
In line 5, i is initialized to 0. So every time when line 6 executes for each new word in the sentence string, the first (and only time as you will see) through the inner for loop, the word is placed in the words array at index 0, i.e. words � Then since you have a "continue firstloop� statement, you break out of the inner for loop and i is never incremented, and thus the code in your for loop is only called once (making it "pointless to have a loop"). So when you continue to loop through the firstloop to get the next word in the sentence, it places the next word again at index zero of the words array. The result is that when you exit your method, and return the array, you have an array with a length of 69 Strings, but only the String at index 0 has anything in it (In this case the String " ." (a space and a period) � the last "word" in the sentence. The rest of the "words" array are null pointers (i.e. they are "empty"). To illustrate this, put the following code just before your return statement:
Notice it prints out the following: 0: " ." 1: "null" 2: "null" 3: "null" 4: "null" 5: "null" ... This shows only words has anything in it. Thus in hashArray(), when you call:
words is null and a NullPointerException is thrown when you try to execute a charAt() method. (This is why the previous line doesn;t throw a NullPointerException but rather just prints the word "null".) If you change that line to:
(noticing I also changed the charAt to 1 since the String only has two characters, a space and a period) It will run and print out the period, since words is not null (the only element in the araay that is not null). So to fix things, you need to use a variable in the toArray() method to track what word it is on:
That will solve your problem, but to be frank, makes for a rather messy and confusing method. Again, as discussed above, the inner for loop only runs one time because of the continue statement. Thus it makes little sense in terms of what it is doing. You only need to put the word into the array once, there is not need for a loop of any kind. Remove the for loop statement (and the closing brace) and leave just the assignment statements. You will find few developers ever use labels and continue statements. It just makes for hard to read code. Many consider them as evil as goto statements. I personally do not like them. Also, as a comment on style and readably, use variable names that are meaningful. This will help make your code easier to read and self-commenting. You did this in most places, but missed one; what is x? what does x mean or represent? Change that variable to something like "numOfSpaces" and it makes things a lot easier to read. Lastly, notice that your code (with my "fix" in place) is not just putting the each word in the array, but a space as well. Run the code and output the contents of the array (with each element inside quotes like I did in the code little snippet above) and you will see that it prints out ...
Notice the leading space in all the array elements, except the first one? Is that the desired state? Or do you need to adjust the index values used in your substring method? Or is a String.trim() call needed? So you are on the right track. However, only a series of assignment statements are needed inside the if statement; no loops are needed inside the it. Try those suggestions and see how it works for you. BTW, you know that you are basically writing the same functionality that exists in the StringTokenizer class, but that is probably just being done as a learning exercise, and you will later learn about the StringTokenizer. [ January 10, 2004: Message edited by: Mark Vender ]
Joined: Nov 18, 2003
Thnx for help ... didn't notice the problem was so simple.