Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NaturalLanguageMultiplier

 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This one is driving me nuts. I can't stop thinking about it. My big problem is when I am trying to get an Object value out of my HashMap. I cast the object to an Integer and then on the next line I use the intValue method to convert to an int. When I run the program that second line causes a nullpointer exception. Any suggestions?
Matthew Phillips
 
Amber Woods
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if I can help or not because I have not been nitpicked for this one yet so you can ignore everything I say.
Here goes: Why not get the object from the hashtable put it to a String and then convert to an int all in one line? At least that is similiar to what I did but whether it is right or even makes sense, I don't know.
 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1820
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't been nitpicked for this assignment yet either (still on 4b), but I have to say it's been my favorite so far.
Is sounds like my code is a lot like Amber's -- I also have a single line that gets an Object from a HashMap, gets its String representation, then uses a static method on Integer to finally produce an int. In my HashMap, both the key and the value are Strings.
I have a couple of ideas, Matthew. (Of course, it's possible that none of these are right.)
(1) Are you sure you have the right syntax for the operations you mention?
I played around using an Integer as the value in the HashMap. If "hashmap" is a HashMap instance and "string" is a String key, then the following syntax produces an int, if the value of "string" Object.equals() a key in "hashmap":
( (Integer)hashmap.get( string ) ).intValue()
Note -- it's very easy to mess up the parentheses when casting and calling a method on the resulting object.
(2) NullPointerExceptions get thrown when you try to call a method on an identifier that is pointing to null, and not to an object. Are you absolutely sure you're passing into your function a key that it can reasonably expect to handle? (Or, in other words, part of the point of the assignment seems to be to get you to throw and catch an Exception for a situation that would produce a NullPointerException if you didn't throw and catch your own.)
(3) Look at the API entry for the get() method on HashMap.
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I've already tried that, but perhaps I had my parens set wrong, because I received a compiler error. Thanks for the suggestions.
Matthew Phillips
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My parens must have caused the compiler problem with the one line conversion to int, but it turned out that the cause of the NullPointerException was a different issue. I forgot to make the static block used to build the HashMap static. Thanks for the help.
Matthew Phillips

[This message has been edited by Matthew Phillips (edited June 13, 2001).]
 
Richard Boren
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matthew, I'm glade you figured it out. I've been trying to get the NullPointerException from the HashMap other than passing get() a String as a key that didn't exist in the map. After Michael's post and what you had already done I figured there had to been some other problem with the code.
Now I have a question. Why do we have to have a static block when putting items in the map if the HashMap object was declared using static to begin with, and why isn't it caught at compile time instead of runtime?
Gee I thought I was done with NarturalLanguageMultiply
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Richard Boren:
Now I have a question. Why do we have to have a static block when putting items in the map if the HashMap object was declared using static to begin with, and why isn't it caught at compile time instead of runtime?

I can answer part of that. The block used to put items into the HashMap is seperate from the HashMap itself. It needs to be static so that it will run when the JVM calls main.
I am not sure about the second part of your question, but I believe it is due to the fact that the block is syntactically correct, but since it is not static it never actually runs when the program runs.
Matthew Phillips
 
Richard Boren
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Matthew Phillips:
I can answer part of that. The block used to put items into the HashMap is seperate from the HashMap itself. It needs to be static so that it will run when the JVM calls main.

Ah! I just assumed any block outside all methods would be automatically executed when the class was loaded and that the NullPointerExection was because the HashMap object could not be reference, not that the hash map was actually empty.

I am not sure about the second part of your question, but I believe it is due to the fact that the block is syntactically correct, but since it is not static it never actually runs when the program runs.
Matthew Phillips[/B]

I think you are correct. I was thinking we should get a "non-static...cannot be referenced...." error message during the compile, but your explanation makes sense.
Thanks, Matthew.
[This message has been edited by Richard Boren (edited June 13, 2001).]
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A non-static block outside all methods would be accessed if and when an object of the class (in this case a NaturalLanguageMultiply object) would be instantiated.

Since the HashMap was declared static, and the initialization block was not, the compiler did not match the two. The HashMap, therefore, remained with its initial default value of "null".

[This message has been edited by Marilyn deQueiroz (edited June 13, 2001).]
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marilyn deQueiroz:
A non-static block outside all methods would be accessed if and when an object of the class (in this case a NaturalLanguageMultiply object) would be instantiated.

Since the HashMap was declared static, and the initialization block was not, the compiler did not match the two. The HashMap, therefore, remained with its initial default value of "null".

[This message has been edited by Marilyn deQueiroz (edited June 13, 2001).]

That is what I discovered when I was debugging. I used a method from the HashMap class to show the contents of my HashMap in the DOS Window and discovered that it was empty.
Matthew Phillips
 
Richard Boren
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The old class-wide versus per object instance gets me again. Will I ever learn.
Excellent explanation Marylin.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic