File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes another NullPointerException problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "another NullPointerException problem" Watch "another NullPointerException problem" New topic

another NullPointerException problem

Juhan Voolaid
Ranch Hand

Joined: Nov 18, 2003
Posts: 179
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 ]
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Hello Juhan, First of all i don't know what you are trying to do here, but i think it'll be more easy to use a StringTokenizer than to loop through the entire String...

In this loop the value of i is never incremented (print its value and you'll see that it is always 0), so you are basicly assigning values to words[0] element only.

The NullPointerException is generated here

because the value of words[3] is null, therefore you can't access an item that does not exist.

Hope this helps.

- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Can you please shorten the string length a little bit this thread is looking very messy.
[ January 10, 2004: Message edited by: Vicken Karaoghlanian ]
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
After a second look at your code... i modified it so it will compile. However i still strongly suggest you use the StringTokenizer class, it will definitly simplify your code.

[ January 10, 2004: Message edited by: Vicken Karaoghlanian ]
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

<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[0] � 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[0] has anything in it. Thus in hashArray(), when you call:

words[3] 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[0] 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 ]
Juhan Voolaid
Ranch Hand

Joined: Nov 18, 2003
Posts: 179
Thnx for help ... didn't notice the problem was so simple.
I agree. Here's the link:
subject: another NullPointerException problem
Similar Threads
Translating c++ into java
method call
Class Cast Exception - Please help
hashing problem
Array losing info in loop