• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The dreaded null pointer exception

 
India Amos
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so the problem is in there somewhere

The stack trace produced by the exception is usually very informative. What does it tell you ?
 
India Amos
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, sorry--


 
K. Tsang
Bartender
Posts: 3348
12
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yessss! Here's the final code:

Given the input

it returns

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic