• 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

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

 
Ranch Hand
Posts: 60
Firefox Browser Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!




 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Noam Ingalls
Ranch Hand
Posts: 60
Firefox Browser Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 60
Firefox Browser Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 10
Opera VI Editor Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
Firefox Browser Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
Firefox Browser Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
Firefox Browser Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic