2

I've a JButton I've encoded with XMLEncoder, and all was well for me, until I tried adding an ActionListener. Now, I get this message

java.lang.InstantiationException: Temp$1
Continuing ...
java.lang.Exception: XMLEncoder: discarding statement JButton.addActionListener(Temp$1);
Continuing ...

Does anyone know why this is happening, or how it can be prevented?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
TheNerdyCoder
  • 184
  • 1
  • 10

1 Answers1

3

Out of the box XMLEncoder can only cope with custom classes that are Java Beans, in particular the class must have a no-argument constructor. Anonymous inner classes such as your Test$1 don't have a no-argument constructor at the bytecode level because the compiler inserts an invisible argument at the start of the constructors' parameter lists to pass in a reference to the "containing instance" of the class that enclosed the inner class.

The java.beans package documentation makes exactly this point, and suggests using java.beans.EventHandler instead of inner classes for listeners.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183