Grails 2.3 domain.encodeAsHTML() throwing null pointer exception from domain.equals()

StackOverflow https://stackoverflow.com/questions/21562673

  •  07-10-2022
  •  | 
  •  

Domanda

I'm not 100% clear where my problem comes from. The issue started when I upgraded from grails 2.2.3 to 2.3.5. I understand that by default (at least, possibly with some Config changes) almost everything now gets encoded as HTML by default, but I also understood that double-encoding was handled and I don't think this is related to double-encoding.

Throughout my codebase many variables are explicitly encodedAsHTML(). After the grails upgrade, this is throwing a NullPointerException.

My trace starts on a simple .gsp with a standard display of a domain object:

${myDomain.encodeAsHTML()}

If I remove the encodeAsHTML(), it works (but since I haven't added the Config bit to encode everything, it doesn't encode).

The next step in the stack trace points me to

Caused by: java.lang.NullPointerException: Cannot get property 'class' on null object
at myPackage.MyDomain$$EOUvovjw.equals(MyDomain.groovy:68)

And this points to my equals() method.

My first point of confusion is "Why is 'encodeAsHTML()' calling the equals() method on my object and comparing it to a null object?"

My second (and probably unrelated, but worth mentioning) comes from examining values inside the equals() method itself:

boolean equals(o)
  {
    log.info("Comparing ${this} to ${o}, o is null? ${o == null}")
    if(this.is(o)) return true
    if(getClass() != o.class) return false    <--- NPE happens here
    ...more stuff...

The log output shows: myPackage.MyDomain Comparing TheValue to null, o is null? false

To note, when I check ${!o} rather than ${o == null} it prints "true", but I'm baffled that (o == null) is false, but I get NPE on o.class

I could update ALL my domain classes .equals() methods to check for (!o) and return false, but I don't believe that is the correct solution.

Many of my domain objects are used in changing persistence contexts which is what led to overriding equals() and hashCode() in the first place. Removing these is not viable.

È stato utile?

Soluzione

I usually use

if (!(o instanceof MyDomain)) return false

This covers null because null isn't an instance of anything, and covers cases where you're comparing incompatible classes.

To give yourself some visibility into why this is getting called, add

new Exception('wahoo!!!').printStackTrace()

at the top of the method. That will dump a stacktrace to the console showing the call stack that led there.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top