You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Scott Arnold <sc...@bjc.org> on 2016/07/13 15:01:15 UTC

Non-Null Object Is Null

I'm new to Groovy (lots of Java experience but almost no Groovy experience) and maybe there is something very basic I am missing here, but I am running into the following issue in a Grails app (but I think it's a not understanding Groovy issue).  I'm not sure I can break it down any further than I already have, so I am kind of stumped about what I am doing wrong here.

In case you know Grails and want some detail on where this code is happening, I'm working on a pre-existing Grails app that calls a service from the BootStrap.groovy during application startup.  The code I'm having a problem with is within the service class.

Here's the code snippet:

System.out.println("is icf null? " + (icf == null));
System.out.println("is icf not null? " + (icf != null));
System.out.println("what is icf? " + icf);

And here's the output:

is icf null? false
is icf not null? true
what is icf? Null

If I try to call a method on icf, I get a NullPointerException.  However, earlier in the code icf is created (currently it is created Java-style rather than using def, as in ICF icf = new ICF()) and the code tries to set several property values within icf (e.g. icf.code = suchandsuch) and none of that throws any errors.

What might be happening here?

Thanks.

Re: Non-Null Object Is Null

Posted by OC <oc...@ocs.cz>.
Scott,

> I guess the moral of the story is that if you use GGTS and suddenly something is acting totally off the wall wacko, try cleaning everything, including deleting all the target folders.

with Java-based languages and toolchains, it is a good idea with about any IDE :)

E.g., my own toolchain brings havoc if one changes package (for the old .class in a wrong place does not get deleted: I still haven't found a reasonably decent way to do that, short of essentially switching off the whole incremental-build thing). And there's a plethora of other and much less obvious problems in the, ahem, let's say exciting, world driven by classloaders.

All the best and congrats you have found the culprit,
OC

On 13. 7. 2016, at 18:33, Scott Arnold <sc...@bjc.org> wrote:

> Good news.  I finally figured this out (mostly).  It was an issue with dirty compiled code.  I am using Groovy Grails Tool Suite (GGTS).  It compiles the application artifacts into two folders, a "target" folder and a "target-eclipse" folder.  I completely deleted both of these folders, did a project clean, a Grails refresh dependencies, and on the next application start the code was working as expected.  
> 
> This was incredibly bizarre and I apologize that it seems to have been more of an IDE problem rather than a Groovy problem.  I guess the moral of the story is that if you use GGTS and suddenly something is acting totally off the wall wacko, try cleaning everything, including deleting all the target folders.
> 
> Thank you everyone who took a look at this.
> 
> -----Original Message-----
> From: Scott Arnold [mailto:sca7740@bjc.org] 
> Sent: Wednesday, July 13, 2016 11:20 AM
> To: users@groovy.apache.org
> Subject: RE: Non-Null Object Is Null
> 
> I decided to experiment a little with immediately after instantiation.  Here is what that looked like:
> 
> ICF icf = new ICF();
> System.out.println("icf is " + icf);
> System.out.println("is it null?  " + (icf == null));
> 
> And the output...
> 
> icf is null
> is it null?  false
> 
> NullPointerException continues to happen if I try to call icf.getClass() at any point.
> 
> Could this have something to do with the code being inside a Groovy-ish loop?  The surrounding code has this:
> 
> def result = client.search(tsr)  // web service call
> for (e in result.entries) {
> 	ICF icf = new ICF();
> 	System.out.println("icf is " + icf);
> 	System.out.println("is it null?  " + (icf == null));
> 	// ... more code
> }
> 
> An additional note -- the earlier capital "N" on null is not occurring -- I think it was an accidental auto-correct in my original message.  It just prints "null" with lower-case "n".
> 
> -----Original Message-----
> From: Scott Arnold [mailto:sca7740@bjc.org] 
> Sent: Wednesday, July 13, 2016 10:35 AM
> To: users@groovy.apache.org
> Subject: RE: Non-Null Object Is Null
> 
> Thought I would add a little more detail.  I'm using Groovy 2.3.7.  I thought this might be somehow related to Groovy's NullObject, but you are supposed to be able to call getClass() on NullObject based on info I have found online.  The very next line of my code is to just call icf.getClass() and that is when the NullPointerException is thrown.
> 
> I'm going to go back and play around with the instantiation some more and see if that adds any more clues.  I'm not sure what the repercussions are of using ICF icf (vs def icf) are.
> 
> -----Original Message-----
> From: Scott Arnold [mailto:sca7740@bjc.org]
> Sent: Wednesday, July 13, 2016 10:01 AM
> To: users@groovy.apache.org
> Subject: Non-Null Object Is Null
> 
> I'm new to Groovy (lots of Java experience but almost no Groovy experience) and maybe there is something very basic I am missing here, but I am running into the following issue in a Grails app (but I think it's a not understanding Groovy issue).  I'm not sure I can break it down any further than I already have, so I am kind of stumped about what I am doing wrong here.
> 
> In case you know Grails and want some detail on where this code is happening, I'm working on a pre-existing Grails app that calls a service from the BootStrap.groovy during application startup.  The code I'm having a problem with is within the service class.
> 
> Here's the code snippet:
> 
> System.out.println("is icf null? " + (icf == null)); System.out.println("is icf not null? " + (icf != null)); System.out.println("what is icf? " + icf);
> 
> And here's the output:
> 
> is icf null? false
> is icf not null? true
> what is icf? Null
> 
> If I try to call a method on icf, I get a NullPointerException.  However, earlier in the code icf is created (currently it is created Java-style rather than using def, as in ICF icf = new ICF()) and the code tries to set several property values within icf (e.g. icf.code = suchandsuch) and none of that throws any errors.
> 
> What might be happening here?
> 
> Thanks.


RE: Non-Null Object Is Null

Posted by Scott Arnold <sc...@bjc.org>.
Good news.  I finally figured this out (mostly).  It was an issue with dirty compiled code.  I am using Groovy Grails Tool Suite (GGTS).  It compiles the application artifacts into two folders, a "target" folder and a "target-eclipse" folder.  I completely deleted both of these folders, did a project clean, a Grails refresh dependencies, and on the next application start the code was working as expected.  

This was incredibly bizarre and I apologize that it seems to have been more of an IDE problem rather than a Groovy problem.  I guess the moral of the story is that if you use GGTS and suddenly something is acting totally off the wall wacko, try cleaning everything, including deleting all the target folders.

Thank you everyone who took a look at this.

-----Original Message-----
From: Scott Arnold [mailto:sca7740@bjc.org] 
Sent: Wednesday, July 13, 2016 11:20 AM
To: users@groovy.apache.org
Subject: RE: Non-Null Object Is Null

I decided to experiment a little with immediately after instantiation.  Here is what that looked like:

ICF icf = new ICF();
System.out.println("icf is " + icf);
System.out.println("is it null?  " + (icf == null));

And the output...

icf is null
is it null?  false

NullPointerException continues to happen if I try to call icf.getClass() at any point.

Could this have something to do with the code being inside a Groovy-ish loop?  The surrounding code has this:

def result = client.search(tsr)  // web service call
for (e in result.entries) {
	ICF icf = new ICF();
	System.out.println("icf is " + icf);
	System.out.println("is it null?  " + (icf == null));
	// ... more code
}

An additional note -- the earlier capital "N" on null is not occurring -- I think it was an accidental auto-correct in my original message.  It just prints "null" with lower-case "n".

-----Original Message-----
From: Scott Arnold [mailto:sca7740@bjc.org] 
Sent: Wednesday, July 13, 2016 10:35 AM
To: users@groovy.apache.org
Subject: RE: Non-Null Object Is Null

Thought I would add a little more detail.  I'm using Groovy 2.3.7.  I thought this might be somehow related to Groovy's NullObject, but you are supposed to be able to call getClass() on NullObject based on info I have found online.  The very next line of my code is to just call icf.getClass() and that is when the NullPointerException is thrown.

I'm going to go back and play around with the instantiation some more and see if that adds any more clues.  I'm not sure what the repercussions are of using ICF icf (vs def icf) are.

-----Original Message-----
From: Scott Arnold [mailto:sca7740@bjc.org]
Sent: Wednesday, July 13, 2016 10:01 AM
To: users@groovy.apache.org
Subject: Non-Null Object Is Null

I'm new to Groovy (lots of Java experience but almost no Groovy experience) and maybe there is something very basic I am missing here, but I am running into the following issue in a Grails app (but I think it's a not understanding Groovy issue).  I'm not sure I can break it down any further than I already have, so I am kind of stumped about what I am doing wrong here.

In case you know Grails and want some detail on where this code is happening, I'm working on a pre-existing Grails app that calls a service from the BootStrap.groovy during application startup.  The code I'm having a problem with is within the service class.

Here's the code snippet:

System.out.println("is icf null? " + (icf == null)); System.out.println("is icf not null? " + (icf != null)); System.out.println("what is icf? " + icf);

And here's the output:

is icf null? false
is icf not null? true
what is icf? Null

If I try to call a method on icf, I get a NullPointerException.  However, earlier in the code icf is created (currently it is created Java-style rather than using def, as in ICF icf = new ICF()) and the code tries to set several property values within icf (e.g. icf.code = suchandsuch) and none of that throws any errors.

What might be happening here?

Thanks.

RE: Non-Null Object Is Null

Posted by Scott Arnold <sc...@bjc.org>.
I decided to experiment a little with immediately after instantiation.  Here is what that looked like:

ICF icf = new ICF();
System.out.println("icf is " + icf);
System.out.println("is it null?  " + (icf == null));

And the output...

icf is null
is it null?  false

NullPointerException continues to happen if I try to call icf.getClass() at any point.

Could this have something to do with the code being inside a Groovy-ish loop?  The surrounding code has this:

def result = client.search(tsr)  // web service call
for (e in result.entries) {
	ICF icf = new ICF();
	System.out.println("icf is " + icf);
	System.out.println("is it null?  " + (icf == null));
	// ... more code
}

An additional note -- the earlier capital "N" on null is not occurring -- I think it was an accidental auto-correct in my original message.  It just prints "null" with lower-case "n".

-----Original Message-----
From: Scott Arnold [mailto:sca7740@bjc.org] 
Sent: Wednesday, July 13, 2016 10:35 AM
To: users@groovy.apache.org
Subject: RE: Non-Null Object Is Null

Thought I would add a little more detail.  I'm using Groovy 2.3.7.  I thought this might be somehow related to Groovy's NullObject, but you are supposed to be able to call getClass() on NullObject based on info I have found online.  The very next line of my code is to just call icf.getClass() and that is when the NullPointerException is thrown.

I'm going to go back and play around with the instantiation some more and see if that adds any more clues.  I'm not sure what the repercussions are of using ICF icf (vs def icf) are.

-----Original Message-----
From: Scott Arnold [mailto:sca7740@bjc.org]
Sent: Wednesday, July 13, 2016 10:01 AM
To: users@groovy.apache.org
Subject: Non-Null Object Is Null

I'm new to Groovy (lots of Java experience but almost no Groovy experience) and maybe there is something very basic I am missing here, but I am running into the following issue in a Grails app (but I think it's a not understanding Groovy issue).  I'm not sure I can break it down any further than I already have, so I am kind of stumped about what I am doing wrong here.

In case you know Grails and want some detail on where this code is happening, I'm working on a pre-existing Grails app that calls a service from the BootStrap.groovy during application startup.  The code I'm having a problem with is within the service class.

Here's the code snippet:

System.out.println("is icf null? " + (icf == null)); System.out.println("is icf not null? " + (icf != null)); System.out.println("what is icf? " + icf);

And here's the output:

is icf null? false
is icf not null? true
what is icf? Null

If I try to call a method on icf, I get a NullPointerException.  However, earlier in the code icf is created (currently it is created Java-style rather than using def, as in ICF icf = new ICF()) and the code tries to set several property values within icf (e.g. icf.code = suchandsuch) and none of that throws any errors.

What might be happening here?

Thanks.

RE: Non-Null Object Is Null

Posted by Scott Arnold <sc...@bjc.org>.
Thought I would add a little more detail.  I'm using Groovy 2.3.7.  I thought this might be somehow related to Groovy's NullObject, but you are supposed to be able to call getClass() on NullObject based on info I have found online.  The very next line of my code is to just call icf.getClass() and that is when the NullPointerException is thrown.

I'm going to go back and play around with the instantiation some more and see if that adds any more clues.  I'm not sure what the repercussions are of using ICF icf (vs def icf) are.

-----Original Message-----
From: Scott Arnold [mailto:sca7740@bjc.org] 
Sent: Wednesday, July 13, 2016 10:01 AM
To: users@groovy.apache.org
Subject: Non-Null Object Is Null

I'm new to Groovy (lots of Java experience but almost no Groovy experience) and maybe there is something very basic I am missing here, but I am running into the following issue in a Grails app (but I think it's a not understanding Groovy issue).  I'm not sure I can break it down any further than I already have, so I am kind of stumped about what I am doing wrong here.

In case you know Grails and want some detail on where this code is happening, I'm working on a pre-existing Grails app that calls a service from the BootStrap.groovy during application startup.  The code I'm having a problem with is within the service class.

Here's the code snippet:

System.out.println("is icf null? " + (icf == null));
System.out.println("is icf not null? " + (icf != null));
System.out.println("what is icf? " + icf);

And here's the output:

is icf null? false
is icf not null? true
what is icf? Null

If I try to call a method on icf, I get a NullPointerException.  However, earlier in the code icf is created (currently it is created Java-style rather than using def, as in ICF icf = new ICF()) and the code tries to set several property values within icf (e.g. icf.code = suchandsuch) and none of that throws any errors.

What might be happening here?

Thanks.

RE: Non-Null Object Is Null

Posted by Scott Arnold <sc...@bjc.org>.
Regarding the toString(), that was a good suggestion for something to check.  Below is the ICF class (I did trim some stuff out that the enterprise might not like me sharing), and it does define a toString() method, but I don't think it can print just "Null" as coded and calling getClass() wouldn't be throwing a NullPointerException, but it is.  

import java.io.Serializable
import java.util.List;
import org.apache.commons.lang.builder.ToStringBuilder

class ICF implements Serializable {

    private static final long serialVersionUID = 1L

	boolean active
	String printName
	String code
	
	String toString() {
        return (new ToStringBuilder(this))
            .append("active", active)
            .append("printName", printName)
            .append("code", code)
            .toString()
	}

}

-----Original Message-----
From: OC [mailto:ocs@ocs.cz] 
Sent: Wednesday, July 13, 2016 10:27 AM
To: users@groovy.apache.org; Scott Arnold <sc...@bjc.org>
Subject: Re: Non-Null Object Is Null

Scott,

I might be wrong, but it looks like your ICF returns a null toString -- your result can be repeated e.g., by this code:

===
class Uhoh {
  String toString() { null }
}
def icf=new Uhoh()
System.out.println("is icf null? " + (icf == null)); System.out.println("is icf not null? " + (icf != null)); System.out.println("what is icf? " + icf); ===

Trick is, toString() is what "print icf" implicitly does. To know more about the object, you might try e.g.,

===
println "icf is some ${icf.getClass()}"
System.out.println("icf is some " + icf.getClass()); // about the same in Javaish ===

All the best,
OC

On 13. 7. 2016, at 17:01, Scott Arnold <sc...@bjc.org> wrote:

> I'm new to Groovy (lots of Java experience but almost no Groovy experience) and maybe there is something very basic I am missing here, but I am running into the following issue in a Grails app (but I think it's a not understanding Groovy issue).  I'm not sure I can break it down any further than I already have, so I am kind of stumped about what I am doing wrong here.
> 
> In case you know Grails and want some detail on where this code is happening, I'm working on a pre-existing Grails app that calls a service from the BootStrap.groovy during application startup.  The code I'm having a problem with is within the service class.
> 
> Here's the code snippet:
> 
> System.out.println("is icf null? " + (icf == null)); 
> System.out.println("is icf not null? " + (icf != null)); 
> System.out.println("what is icf? " + icf);
> 
> And here's the output:
> 
> is icf null? false
> is icf not null? true
> what is icf? Null
> 
> If I try to call a method on icf, I get a NullPointerException.  However, earlier in the code icf is created (currently it is created Java-style rather than using def, as in ICF icf = new ICF()) and the code tries to set several property values within icf (e.g. icf.code = suchandsuch) and none of that throws any errors.
> 
> What might be happening here?
> 
> Thanks.


Re: Non-Null Object Is Null

Posted by OC <oc...@ocs.cz>.
Scott,

I might be wrong, but it looks like your ICF returns a null toString -- your result can be repeated e.g., by this code:

===
class Uhoh {
  String toString() { null }
}
def icf=new Uhoh()
System.out.println("is icf null? " + (icf == null));
System.out.println("is icf not null? " + (icf != null));
System.out.println("what is icf? " + icf);
===

Trick is, toString() is what "print icf" implicitly does. To know more about the object, you might try e.g.,

===
println "icf is some ${icf.getClass()}"
System.out.println("icf is some " + icf.getClass()); // about the same in Javaish
===

All the best,
OC

On 13. 7. 2016, at 17:01, Scott Arnold <sc...@bjc.org> wrote:

> I'm new to Groovy (lots of Java experience but almost no Groovy experience) and maybe there is something very basic I am missing here, but I am running into the following issue in a Grails app (but I think it's a not understanding Groovy issue).  I'm not sure I can break it down any further than I already have, so I am kind of stumped about what I am doing wrong here.
> 
> In case you know Grails and want some detail on where this code is happening, I'm working on a pre-existing Grails app that calls a service from the BootStrap.groovy during application startup.  The code I'm having a problem with is within the service class.
> 
> Here's the code snippet:
> 
> System.out.println("is icf null? " + (icf == null));
> System.out.println("is icf not null? " + (icf != null));
> System.out.println("what is icf? " + icf);
> 
> And here's the output:
> 
> is icf null? false
> is icf not null? true
> what is icf? Null
> 
> If I try to call a method on icf, I get a NullPointerException.  However, earlier in the code icf is created (currently it is created Java-style rather than using def, as in ICF icf = new ICF()) and the code tries to set several property values within icf (e.g. icf.code = suchandsuch) and none of that throws any errors.
> 
> What might be happening here?
> 
> Thanks.