wood burning stoves*
The moose likes Beginning Java and the fly likes What causes this null pointer exception in SortedMap/TreeMap? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What causes this null pointer exception in SortedMap/TreeMap?" Watch "What causes this null pointer exception in SortedMap/TreeMap?" New topic
Author

What causes this null pointer exception in SortedMap/TreeMap?

Charles Bradley
Greenhorn

Joined: Oct 04, 2009
Posts: 25

I'm new to Java, new to Netbeans where this code was developed, and new to JUnit. The SortedMap code came from examples scattered around the web. I think I understand the various levels of abstraction and TreeMap as the implementation. I've trimmed away most of the tests and the methods that iterate through the TreeMap. All of the tests fail with a null pointer exception. I tried to isolate the problem with some System.out.println statements. The 4, 5, and key= lines print. Originally, getItem just called get; the call to containsKey and other stuff was added to try to isolate the problem. It seems to me the error is in how I call any of the methods, but I am blind to what is wrong.
I stripped out package and include statements from my AllItems class, and all the empty setup and teardown methods from the testing class.



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Which line is causing it? Please show us the stack trace.
Charles Bradley
Greenhorn

Joined: Oct 04, 2009
Posts: 25
Caused by line 8 of AllItems, called from line 20 of the test.
When the routine was simpler, just the call on line 10, then the error was caused by line 10.
I know what a stack trace is, but I'll have to find out how to get one. I'll post it ASAP.

Update: There is no stack trace. JUNIT handles the exception, continues, and reports the problem when it is done.
When I run the test, NetBeans tells me one or more projects compiled with errors. But, there are no compile errors in any of my classes or tests. I checked.
This is perhaps unusual, but intentional. The key is one field of an Item, and the value is the entire Item.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617


That line doesn't do anything except create a TreeMap, which is promptly thrown away at the end of the constructor.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
I think itemsList is null in line 8 because, as David Newton points out, you create something new and don't do anything with it.

I think that new TreeMap is supposed to be assigned to the field itemsList. In the constructor line which David pointed out.

By the way: that is not ideal naming. It would be better to call it itemsMap.
Charles Bradley
Greenhorn

Joined: Oct 04, 2009
Posts: 25
Thanks. Thanks very much. Adding the assignment to the constructor makes all the tests work correctly.
I also changed the name from itemsList to items instead of itemsMap, avoiding any false or true clue about the implementation.
I'm sure glad I did not trim out the constructor from the original posting. My lesson: don't be so sure you know where the problem is.
Thanks again.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
You're welcome. Remember the constructor is one of the places you prevent NPEs.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: What causes this null pointer exception in SortedMap/TreeMap?
 
Similar Threads
java interface methods cannot have body?
Using a sorted map...
Collection problem
Command Line compiling
Struts logic:iterate weirdness