• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

The dreaded null pointer exception

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, sorry--


 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yessss! Here's the final code:

Given the input

it returns

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic