File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes XMLEncoder - Stackoverflowerror - overriding equals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "XMLEncoder - Stackoverflowerror - overriding equals" Watch "XMLEncoder - Stackoverflowerror - overriding equals" New topic

XMLEncoder - Stackoverflowerror - overriding equals

Jonathan Janisch

Joined: Mar 17, 2007
Posts: 24
Hi, I'm trying to write out a class to XML using XMLEncoder.

There's a few unique things about this class.

1) It has no default constructor
2) I'm overriding the equals method.
3) The class has a static int count which defaults to 0. Each object is assigned an ID based on the current count. The ID is used to compare two objects.

Since it has no default constructor, I'm using the defaultpersistencedelegate. Here's the code:

If you run this, you will get a stackoverflowerror (atleast under jdk1.6.0_01 on Windows). I'm not an XMLEncoder expert, I've narrowed the problem down and I came up with this simple example.

The combination of the static int ID and override equals seems to be the problem. If you change the equals to:

It will still give the same error. However, if you remove the equals method completely, it will work fine and the xml output is as I expected. But unfortunately, I need to override the equals method.

1) Does anyone know how I can fix this?
2) Why would "return super.equals(obj)" give different behavior than having no equals method at all?

Note: I realize if my class were really a Person class, the equals method should compare SSN, first and last name, etc. and not some made up int ID - this is just a simple example to demonstrate the problem.

Thank you!
Jonathan Janisch

Joined: Mar 17, 2007
Posts: 24
I solved it using my own persistence delegate to create a new Person. I don't understand why I had to do this, since I thought that was the purpose of the defaultpersistencedelegate. I did see some stuff in the source for defaultpersistencedelegate that involved checks for overriding equals - but I won't pretend like I understand how it works.

I agree. Here's the link:
subject: XMLEncoder - Stackoverflowerror - overriding equals
It's not a secret anymore!