aspose file tools*
The moose likes Beginning Java and the fly likes HashMap values-- I think I know why but how do I fix this null pointer exception? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HashMap values-- I think I know why but how do I fix this null pointer exception?" Watch "HashMap values-- I think I know why but how do I fix this null pointer exception?" New topic
Author

HashMap values-- I think I know why but how do I fix this null pointer exception?

Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

I'm writing an interpreter for an assignment. Trying to map these keys with a HashMap-- program code is stored line by line in an ArrayList, the first word/token in the String is always a command, and subsequent tokens in the string are the instructions data that need to be passed into the class mapped for execution. I'm testing one of those classes now and I'm getting a NullPointerException message. Did some debugging and found that somehow thread of execution's goes from breakpoint 0 to breakpoint1 but then jumps straight to breakpoint 3 and skips breakpoint 2, which is most likely causing the NullPointerException. The thing is I'm not sure why the code at breakpoint 2 isn't coming into effect. Cound someone take a look and tell me why it's not working? The way I thought this was supposed to work was that it'd read the Strings in the ArrayList one by one, and then I'd use String.split to split the command from the instruction data; the indexOf() was to denote how long the command string should be. Is there something in the String.indexOf() method I've overlooked? Thanks!





"How do you define fool?"
"I don't attempt it. I wait for demonstrations. They inevitably surpass my imagination."
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Noam Ingalls wrote:Did some debugging and found that somehow thread of execution's goes from breakpoint 0 to breakpoint1 but then jumps straight to breakpoint 3 and skips breakpoint 2, which is most likely causing the NullPointerException.


Afraid that doesn't make sense. If line 5 is throwing an exception then neither breakpoint 2 or breakpoint 3 would be reached.

Also, are you sure line 17 in your code is correct ?

Again also, rather than just printing breakpoint x why don't you print the values of some relevant variables.


Joanne
Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

Um yeah, I changed things around some since then, but now the error's at line 72. I'm getting rather frustrated with this whole thing; just need to tokenize the string into 2 parts, and send the second part of the string to the right class, why is this being so difficult?

Breakpoints with error message:
break0
break1
break3
Exception in thread main java.lang.NullPointerException at StringSplit.main(StringSplit.java:72)



Full code:

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Noam Ingalls wrote:the error's at line 72

So either directive is null or the execute method is throwing the exception. The stack trace will tell you which it is.
And if it's the first then you need to fix the logic error on line 65.

And once you've fixed that, the problem will become very obvious if you print out the value of listString at line 50 and change lines 52 and 54 to print out the values of firstToken and command respectively.
Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

I tried that again: the execute() method is throwing the exception now; line 69. Still skipping past breakpoint 2.

Debugging output:

break0
#A TPL HELLO WORLD PROGRAM command
#A TPL HELLO WORLD PROGRAM break1
break3
Exception in thread "main" at java.lang.NullPointerException at StringSplit.main(StringSplit.java:69)


Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Noam Ingalls wrote:I tried that again: the execute() method is throwing the exception now; line 69.

I would put good money on that not being true. The NPE is being thrown because directive is null.
You still haven't fixed the problem on line 67 - look at what you are checking on that line.
Saikat Roy Mahasay
Greenhorn

Joined: Jan 12, 2012
Posts: 10

I do not see you putting anything into the commandHash. Well there's a put, but it is in the constructor block ( the { ... } part right into a class is a constructor block) and it is not being called as you have not instantiated the StringSplit class.

So you have your first command "# A TPL HELLO WORLD PROGRAM" and your HashMap is empty. so your call to get returns a null, which naturally, you can't call the execute method on.

Even if you could initialize the class with a call to constructor, the HashMap will have only the EndStatement. And you still won't get any Directive from it. Hence, when you call execute on it, it will throw a NPE.

Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

Saikat Roy Mahasay wrote:I do not see you putting anything into the commandHash. Well there's a put, but it is in the constructor block ( the { ... } part right into a class is a constructor block) and it is not being called as you have not instantiated the StringSplit class.

So you have your first command "# A TPL HELLO WORLD PROGRAM" and your HashMap is empty. so your call to get returns a null, which naturally, you can't call the execute method on.

Even if you could initialize the class with a call to constructor, the HashMap will have only the EndStatement. And you still won't get any Directive from it. Hence, when you call execute on it, it will throw a NPE.



Man, thanks-- I've been wondering all afternoon just what went wrong with it! So how do I dump stuff into the commandHash? Instantiate the StringSplit class? But StringSplit has the main method already...?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Take everything that is currently in your main method and put it into a new non static method (lets call it parseCommands).
Then change your main method to


And change the declaration of commandHash so that it is not static.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Did you check below line of Joanne's post?
You still haven't fixed the problem on line 67 - look at what you are checking on that line.

You are checking null for the commandHash reference - what should you be checking instead of that? Note that break3 got printed after a method call on commandHash which means commandHash is not null and need not be checked again.

<Edit - Removed wrong information in my post>
Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

John Jai wrote:Did you check below line of Joanne's post?

You are checking null for the commandHash reference - what should you be checking instead of that? Note that break3 got printed after a method call on commandHash which means commandHash is not null and need not be checked again.

To be honest, I'm not quite sure: when I wrote that I thought I'd need to check if the substring being passed to the HashMap exists in it as a key or not, right? But looks like that's not the way to do it?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Non-static methods called using a reference variable that points to null will result in a NullPointerException.

In the below code, I have inserted Italy as key and it's capital Rome as its value in the map2. Now I try to get the capital of Spain which will return null.

So what you think should be tested against null? See I am wrongly checking against map2 and calling a method using the capital reference that points to null.

It's similar to what you have - you call a method on a reference pointing to null but null check a different reference.

Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

Oh sweet clover fields-- FINALLY fixed it with a load of trial and error-- it was the directive I had to check! Thanks so much for the help people! One more thing though: I've got 8 potential keywords that this has to be able to find and map-- how do I modify this indexOf() to allow for that?

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Noam Ingalls wrote:Oh sweet clover fields-- FINALLY fixed it with a load of trial and error-- it was the directive I had to check!

I knew if we kept mentioning it, you'd spot it eventually

Noam Ingalls wrote:One more thing though: I've got 8 potential keywords that this has to be able to find and map-- how do I modify this indexOf() to allow for that?

See my answer in your other post
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMap values-- I think I know why but how do I fix this null pointer exception?
 
Similar Threads
below code is correct way to write the code in generic way if we receive different xml formats
HashMap implementation for an interpreter: cannot find symbol error
Help with this code!
creating arrays from data in a text file
Global Array not working