File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes hashMap question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "hashMap question" Watch "hashMap question" New topic

hashMap question

rick simpson

Joined: Oct 15, 2008
Posts: 8

I have the following class:

I get an NullPointerExpection when I try using the recordEntry method from a jUnit test case. The constructor works fine. The hand off to the method is fine. Problem is at the put.

Here's the jUnit test:

Any suggestions welcomed. I'm stumped.

[edit]Add code tags. CR[/edit]
[ October 15, 2008: Message edited by: Campbell Ritchie ]
Paul Campbell
Ranch Hand

Joined: Oct 06, 2007
Posts: 338
In your code

you have only created a reference variable called entries to store the reference to the HashMap object... you still haven't created the entries object on the heap until you peform

[ October 15, 2008: Message edited by: Paul Campbell ]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45255
Welcome to JavaRanch, Rick Simpson

You will have to get into the habit of initialising every single field in a constructor, then you will get far fewer NullPointerExceptions.

By the way: We have a button marked CODE; please use it around code; I have edited your post to show the effect, that it preserves indentation and makes the code much nicer to read.
I agree. Here's the link:
subject: hashMap question
It's not a secret anymore!