Issue Details (XML | Word | Printable)

Key: GROOVY-1922
Type: Bug Bug
Status: Closed Closed
Resolution: Won't Fix
Priority: Major Major
Assignee: Jochen Theodorou
Reporter: Eric McIntyre
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
groovy

NPE from classes that extend AbstractMap

Created: 30/May/07 02:04 PM   Updated: 02/Jul/07 05:28 AM
Component/s: None
Affects Version/s: 1.1-beta-2
Fix Version/s: 1.1-beta-2

Time Tracking:
Not Specified

File Attachments: 1. File WeirdNPETest.groovy (0.3 kB)



 Description  « Hide
A NullPointerException is being thrown from custom subclasses of AbstractMap in the following scenario:

1) Class does not override Object get(Object key)
2) Method is called that uses a closure that references a class variable, instance variable, or property of the class

The attached test file duplicates the problem.



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Eric McIntyre added a comment - 30/May/07 02:08 PM
In the attached script I have tried extending AbstractMap, HashMap, and LinkedHashMap – all of which exhibited the same behavior unless get() was implemented. I also tried extending other classes, like AbstractList, Random, and Button, and implementing Map directly, but these worked.

Eric McIntyre added a comment - 30/May/07 02:11 PM
It's also important to note that this script runs fine in Groovy 1.0. It breaks with 1.1 Beta 1 and above (tested with beta 1 and a very recent beta 2 snapshot build from Canoo, 20070529165022).

Jochen Theodorou added a comment - 24/Jun/07 04:08 PM
hmm... when you replace "var" with this.@var, then all is fine...

Jochen Theodorou added a comment - 02/Jul/07 05:28 AM
after looking at this closely I will close it with "Won't Fix"

In your code

def doIt() {
		assert 'o' == var.substring(1, 2)
		def c = { assert 'o' == var.substring(1, 2) }
		c()
	}

the first line succeeds the closure fails. the reason is that in a closure getProperty is always used and since this is a Map, the map contents are used which means var is null. I see two basic ways out of it... First use "this.@var.substring(1, 2) }" in the closure. You will then access the attribute directly. Next version is to remove "def var='foo'" and instead write a constructor with "var='foo'". Then var will be set in the map with the value "foo" and accessing it will not cause a NPE.