This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 37897
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
Similar Threads
JSF 2.0 and AJAX Problem
Immutable Class
A Simple Test Framework using JUnit and Spring (Part 1)
Could the static keyword cause a bottleneck/ deadlock?
JUnit and AbstractTestCase Problem