aspose file tools*
The moose likes Beginning Java and the fly likes The dreaded null pointer exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The dreaded null pointer exception" Watch "The dreaded null pointer exception" New topic
Author

The dreaded null pointer exception

India Amos
Greenhorn

Joined: Apr 12, 2009
Posts: 5
Greetings, jolly ranchers.

I've been beating my head against this small program for several hours now, and no matter how I rewrite it, I keep getting an error on run (it compiles fine):



It's a word-counter that I'm trying to make case-insensitive, probably the hard way. With that middle block commented out, as shown, it runs, so the problem is in there somewhere, and I'm sure it's glaringly obvious to anyone who actually has a clue what he or she is doing. I would greatly appreciate any help you can offer.

Thanks!

India
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

so the problem is in there somewhere

The stack trace produced by the exception is usually very informative. What does it tell you ?


[My Blog]
All roads lead to JavaRanch
India Amos
Greenhorn

Joined: Apr 12, 2009
Posts: 5
Ah, sorry--


K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2368
    
    7

Welcome to the Ranch.

Ok let me guess why you have 2 maps. The word map is the actual or source and the word2 map is the lowercase version. And let me guess why you get the NullPointerException... either the words or word2 is null for the else if block.

Anyway since you are doing a "word counter" you really need to define what is a "word". Suppose I have the line or sentence: "Today is sunny." This line has 3 words. So if I assume you put the items into the map correctly you should get:

And when you fetch say "is" you get 2.

But I personally think there can be 2 ways of doing this. It depends on what you really want at the end - just a number or like what you have done (matching word count to the actual word). If you just want a number - given you read a line you can simply split it using the blank or space into a String array and return the array size. If keeping what you have done you can again return the map size.

For the other way putting the words into the map is the key. Once you can try looping the keys and see if it is want you want.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16


What will happen when you have found "A" the first time ?
You will have "A" in words, and "a" in words2.
Then what will happen when "a" comes ?
India Amos
Greenhorn

Joined: Apr 12, 2009
Posts: 5
Thanks, K.

Yes, I'm trying to get a separate count for each word, but without doubling anything up (e.g., having separate counts for "the" and "The") and without losing all the capitalization (e.g., "finland").

If i put in the text

I want the two hashmaps to contain the values


Though . . . I think I'm having a logic problem, as well. Ultimately, I'd like to get this list:



If a word is capitalized on its first appearance, it should stay capitalized but be counted together with any subsequent lowercase appearances.
India Amos
Greenhorn

Joined: Apr 12, 2009
Posts: 5
Okay! For the benefit of those who come after . . .

My friend Christina pointed out this morning that,
You’re getting the error in line 33 because you’re invoking words.get(t), but you’re in the else block where words.containsKey(t) is false.

Then she added,
I think what you really want is not 2 maps w/ count, but 1 map of lowercase -> count and 1 map of lowercase -> first seen version of string.

I'm sure she's right. I'll post the final code if I get it working.
India Amos
Greenhorn

Joined: Apr 12, 2009
Posts: 5
Yessss! Here's the final code:

Given the input

it returns

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The dreaded null pointer exception