groovy

NPE from classes that extend AbstractMap

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Won't Fix
  • Affects Version/s: 1.1-beta-2
  • Fix Version/s: 1.1-beta-2
  • Component/s: None
  • Labels:
    None
  • Number of attachments :
    1

Description

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.

Activity

Hide
Eric McIntyre added a comment -

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.

Show
Eric McIntyre added a comment - 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.
Hide
Eric McIntyre added a comment -

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).

Show
Eric McIntyre added a comment - 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).
Hide
blackdrag blackdrag added a comment -

hmm... when you replace "var" with this.@var, then all is fine...

Show
blackdrag blackdrag added a comment - hmm... when you replace "var" with this.@var, then all is fine...
Hide
blackdrag blackdrag added a comment -

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.

Show
blackdrag blackdrag added a comment - 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.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: