[groovy-user] Accessing Domain-Object Property

2 views
Skip to first unread message

Lorenz Graf

unread,
Nov 14, 2007, 4:42:37 PM11/14/07
to us...@groovy.codehaus.org
Hello there,
I'm a complete newbie to Groovy and I'm still struggling with some basics.

I've got a Domain-Object called File with has a typesave List as Property.
Now I'm trying to access and fill this list from a class outside. But my unittests always get a Nullpointer Exception ( java.lang.NullPointerException: Cannot invoke method leftShift() on null object).

Here are the code snippets:

class File {
    List<Index> indices
}

I'm trying to access the list as follows:

File f = new File()
//@ this point the list is Null
f.indices << new Index(key: '', value: '')

I've also tried to to initialize the list at the File object in several ways.
List<Index> indices = []
List<Index> indices = new ArrayList<Index>()

The last line is pretty much the java way and not particularly how I'd like to do it.

Now my two questions to that:

1. Why do I get that Nullpointer Exception when I access the List and
2. How can I initialize a type save list with groovy generics?

Anyone a little hint for a newbie?
Thanks a lot.



Loree

unread,
Nov 14, 2007, 5:10:30 PM11/14/07
to us...@groovy.codehaus.org

Hello there,
Could it be that it has to do with the fact that my unittests are written in
java so the jointcompilation?

Cheers

--
View this message in context: http://www.nabble.com/Accessing-Domain-Object-Property-tf4808229.html#a13757620
Sent from the groovy - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Russel Winder

unread,
Nov 15, 2007, 4:21:50 AM11/15/07
to us...@groovy.codehaus.org

On Wed, 2007-11-14 at 22:42 +0100, Lorenz Graf wrote:
> Hello there,
> I'm a complete newbie to Groovy and I'm still struggling with some
> basics.
>
> I've got a Domain-Object called File with has a typesave List as
> Property.
> Now I'm trying to access and fill this list from a class outside. But
> my unittests always get a Nullpointer Exception
> ( java.lang.NullPointerException: Cannot invoke method leftShift() on
> null object).
>
> Here are the code snippets:
>
> class File {
> List<Index> indices

This creates a variable which is a reference to something. You need to
create an object to be referred to:

List<Index> indices = [ ]

> }
>
> I'm trying to access the list as follows:
>
> File f = new File()
> //@ this point the list is Null
> f.indices << new Index(key: '', value: '')

This will definitely create a NullPointerException since f.indices is
null.

> I've also tried to to initialize the list at the File object in
> several ways.
> List<Index> indices = []
> List<Index> indices = new ArrayList<Index>()

You need to have the initialization inside the class.

> The last line is pretty much the java way and not particularly how I'd
> like to do it.
>
> Now my two questions to that:
>
> 1. Why do I get that Nullpointer Exception when I access the List and

because f.indices in null ;-)

> 2. How can I initialize a type save list with groovy generics?

See above. And remember type safety of generics is a compile time not a
run time thing -- so real type safety on the JVM is a bit of an
delusion.

--
Russel.
====================================================
Dr Russel Winder Partner

Concertant LLP t: +44 20 7193 9203
41 Buckmaster Road, f: +44 8700 516 084
London SW11 1EN, UK. m: +44 7770 465 077

signature.asc

Loree

unread,
Nov 19, 2007, 4:58:14 PM11/19/07
to us...@groovy.codehaus.org

Hello again,
I' tried it out as Russel told me.
Now the weird thing is, that if I initialize the List as explained, the
unittests runned in IntelliJ still fail with the nullpointer exception
below. But i f I run the tests with my gant-script, the tests passes. What'
s the difference between running unittests in IntelliJ and in the
gant-script? I've listed the gant part below.

target('test': 'Runs the Unit Tests') {
depends(compile)

Ant.junit(printsummary: 'yes', failureproperty:
unit_test_failure_property) {
formatter(type: 'plain')
batchtest(fork: 'yes', todir: buildOutputClasses) {
fileset(dir: buildOutputClasses, includes: '**/*Test.class')
}
classpath {
pathelement(location: buildOutputClasses)
path(refid: 'compile_classpath')
}
}

Ant.fail(message: 'unit tests consist failures', if:
unit_test_failure_property)
}

--
View this message in context: http://www.nabble.com/Accessing-Domain-Object-Property-tf4808229.html#a13846287

Rasmus Höglund

unread,
Nov 19, 2007, 5:44:30 PM11/19/07
to us...@groovy.codehaus.org
I found a bug in 1.1-rc2

int a = 1
println (-a)

=>

Exception in thread "main" java.lang.NoSuchMethodError:
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.negate(Ljava/lang/Object;)Ljava/lang/Object;

no negate method for numbers...

workaround is to do:
println (0-a)

I'm probably not the first to find this, I'll post it just to be sure
though...


regards,
Rasmus

Andrew Gaydenko

unread,
Nov 19, 2007, 5:49:39 PM11/19/07
to us...@groovy.codehaus.org
Rasmus,

Build 879 prints -1 for me: http://build.canoo.com/groovy/


Andrew

======= On Tuesday 20 November 2007, Rasmus Höglund wrote: =======

Rasmus Höglund

unread,
Nov 19, 2007, 6:16:33 PM11/19/07
to us...@groovy.codehaus.org
How do I download the build? Don't see a link for it on that page... or
do I need to build it from the source myself?

Hm, the strange thing is that on the same version it runs in the
console, and when running just groovy Test... but when it's compiled
inside eclipse (with groovy-all-1.1-rc-2.jar on the class path) I get
the error.

Did some testing, and when compiling from the command line and running
it with java then it works. Must be that the groovy plugin for eclipse
is somehow mixing up the versions when it's compiling. I've seen that it
comes with an embedded groovy jar, I removed it from the classpath and
added 1.1-rc2 instead... but I didn't see anywhere to put the path to
groovyc. I might have to do a build script instead of relying on the
plugin I suppose..

-rasmus


Andrew Gaydenko skrev:

Andrew Gaydenko

unread,
Nov 19, 2007, 6:39:10 PM11/19/07
to us...@groovy.codehaus.org
======= On Tuesday 20 November 2007, Rasmus Höglund wrote: =======
> How do I download the build? Don't see a link for it on that page... or

Build Artifacts - Deliverables

> do I need to build it from the source myself?

There are different archives. I use groovy-binary-1.1-final-SNAPSHOT.zip.

Paul King

unread,
Nov 19, 2007, 7:14:38 PM11/19/07
to us...@groovy.codehaus.org

The compiler and runtime now (including RC2 I believe) use unaryMinus() not
negate().
You will get the error below if you compile with (or have an IDE that is
using) an older
version to compile but then attempt to use the latest Groovy runtime when
running.

Paul.

--
View this message in context: http://www.nabble.com/Accessing-Domain-Object-Property-tf4808229.html#a13848420


Sent from the groovy - user mailing list archive at Nabble.com.

Alex Shneyderman

unread,
Nov 20, 2007, 3:20:22 AM11/20/07
to us...@groovy.codehaus.org
On Nov 14, 2007 10:42 PM, Lorenz Graf <loren...@gmail.com> wrote:
> Hello there,
> I'm a complete newbie to Groovy and I'm still struggling with some basics.
>
> I've got a Domain-Object called File with has a typesave List as Property.
> Now I'm trying to access and fill this list from a class outside. But my
> unittests always get a Nullpointer Exception (
> java.lang.NullPointerException: Cannot invoke method leftShift() on null
> object).
>
> Here are the code snippets:
>
> class File {
> List<Index> indices
> }
>
> I'm trying to access the list as follows:
>
> File f = new File()
> //@ this point the list is Null
> f.indices << new Index(key: '', value: '')
>
> I've also tried to to initialize the list at the File object in several
> ways.
> List<Index> indices = []
> List<Index> indices = new ArrayList<Index>()
>
> The last line is pretty much the java way and not particularly how I'd like
> to do it.
>
> Now my two questions to that:
>
> 1. Why do I get that Nullpointer Exception when I access the List and

my guess to this one is that you are creating java.io.File instead of your
domain class called File. java.io package is automatically imported in
Groovy. Try this to make sure you have correct class:

File f = new File()

println f.getClass().getName()

Rasmus Höglund

unread,
Nov 20, 2007, 3:01:25 PM11/20/07
to us...@groovy.codehaus.org
Thank you for clarifying this!

-rasmus

Paul King skrev:

saw303

unread,
Nov 21, 2007, 2:27:49 PM11/21/07
to us...@groovy.codehaus.org

Hello Alex


Alex Shneyderman wrote:
>
> my guess to this one is that you are creating java.io.File instead of your
> domain class called File. java.io package is automatically imported in
> Groovy. Try this to make sure you have correct class:
>
> File f = new File()
> println f.getClass().getName()
>

Thank you for your answer. We already have tried to rename the domain object
to MFile but the strange behaviour remained. The thing is that if we run the
unit test from our "DOS"-Shell using GAnt everything works perfect. We also
tried to reproduce the behaviour in the groovyShell but everything was
perfect (see listing 1).

We also configured IntelliJ according to the junit testing manual
(http://groovy.codehaus.org/Unit+Testing) and used groovy.util.AllTestSuite
to run our tests. See listing 2 to get an impression of our unit test. All
groovy classes are located in package 'ch.minsight.core.groovy.domain'. In
listings 3 and 4 you see exception stacktrace occurring during testing with
IntelliJ.

Does anyone have a clue whats wrong about our IntelliJ configuration? This
one somehow gets frustrating :(

Best regards
Silvio

[Listing 1 - Run this in groovyConsole]
class Index implements Serializable {
String key;
String value;
}

class File implements Serializable{
String name
String path
List<Index> indices = [ ]
}

def f = new File()
assert 0 == f.indices.size()

[Listing 2 - Unit test]
import ch.minsight.core.groovy.domain.File

class FileTest extends GroovyTestCase {

File f;

void setUp() {f = new File(name: 'file.txt', path: '/my/path/to/a/file',
indices: [])}

void tearDown() {f = null}

void testIndicesListIsInitiallyEmpty()
{
assertEquals('List size is not correct', 0, f.indices.size())
}

void testConstructor()
{
def f = new File(name: 'myfile.txt', path: '/home/me', indices: [ ])
assertEquals('filename does not match', 'myfile.txt', f.name)
assertEquals('path does not match', '/home/me', f.path)
assertEquals('List size is not correct', 0, f.indices.size())
}

void testAddOneItem()
{
f.indices << new Index(key: 'hello', value: 'world')
assertEquals('the list contains our index object', [new Index(key:
'hello', value: 'world')], f.indices)
}
}

[Listing 3 - Comparison Exception of Test 'testConstuctor']
junit.framework.ComparisonFailure: filename does not match
Expected :myfile.txt
Actual :
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at
org.codehaus.groovy.runtime.metaclass.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:56)
at
org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:538)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:803)
at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrentN(ScriptBytecodeAdapter.java:78)
at
ch.minsight.core.groovy.domain.FileTest.testConstructor(FileTest.groovy:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)

[Listing 4 - NPE of Test 'testIndicesListIsInitiallyEmpty']
ava.lang.NullPointerException: Cannot invoke method size() on null object
at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:77)
at org.codehaus.groovy.runtime.Invoker.invokePogoMethod(Invoker.java:102)
at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:79)
at
org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:69)
at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:170)
at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethod0(ScriptBytecodeAdapter.java:213)
at
ch.minsight.core.groovy.domain.FileTest.testIndicesListIsInitiallyEmpty(FileTest.groovy:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
--
View this message in context: http://www.nabble.com/Accessing-Domain-Object-Property-tf4808229.html#a13884199


Sent from the groovy - user mailing list archive at Nabble.com.

saw303

unread,
Nov 21, 2007, 6:21:59 PM11/21/07
to us...@groovy.codehaus.org

Hello everyone


saw303 wrote:
>
> Does anyone have a clue whats wrong about our IntelliJ configuration?
>

Currently I am working with

IntelliJ Version Selena #7364
JetGroovy Plugin 0.1.12084

running under Windows Vista (problem also occurs with Windows XP SP2)

I have discovered more about our IntelliJ problem and tried to explain it
with a small movie.

http://minsight.googlecode.com/files/jetgroovy-bug.avi Here is a
documentation to my explanation

We wrote a very simple POGO and a GroovyTestCase. Whenever we change
something within the TestCase Junit fails to execute our test correctly
(please see the 'movie'). if we are going to change something in the POGO
and run the test again everything turn 'green'.

Can anyone tell me if this is a JetGroovy Plugin bug or am I getting
something wrong. Thank you very much for your support.

Best regards,
Silvio

--
View this message in context: http://www.nabble.com/Accessing-Domain-Object-Property-tf4808229.html#a13888086

Randall R Schulz

unread,
Nov 21, 2007, 6:37:27 PM11/21/07
to us...@groovy.codehaus.org
On Wednesday 21 November 2007 15:21, saw303 wrote:
> ...

>
> Currently I am working with
>
> IntelliJ Version Selena #7364
> JetGroovy Plugin 0.1.12084

There are currently three newer releases of JetGrovy.


> ... Thank you very much for your support.

It's not support, but I recommend that you use the JetGroovy Forum to
discuss JetGroovy problems and feature requests:

<http://www.intellij.net/forums/forum.jspa?forumID=74>

Other IntelliJ / JetBrains forums:

<http://www.intellij.net/forums/index.jspa>


> Best regards,
> Silvio


Randall Schulz

saw303

unread,
Nov 22, 2007, 3:33:02 AM11/22/07
to us...@groovy.codehaus.org

According to the IntelliJ forum

http://www.intellij.net/forums/thread.jspa?messageID=5202479&#5202479

> "This is intellij core bug. Please wait for 7.0.2 for a fix. "
>
--
View this message in context: http://www.nabble.com/Accessing-Domain-Object-Property-tf4808229.html#a13892752


Sent from the groovy - user mailing list archive at Nabble.com.

Reply all
Reply to author
Forward
0 new messages